Source of file class-data.php

Size: 2,421 Bytes - Last Modified: 2016-01-05T15:40:14-05:00

../src/lib/class-data.php

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
Covered by 1 test(s):
  • WpWodify\DataTest::test_set_api
50
Covered by 1 test(s):
  • WpWodify\DataTest::test_set_api
515253545556575859
Covered by 1 test(s):
  • WpWodify\DataTest::test_get_api
6061626364656667686970
Covered by 1 test(s):
  • WpWodify\DataTest::test_set_cache
71
Covered by 1 test(s):
  • WpWodify\DataTest::test_set_cache
727374757677787980
Covered by 1 test(s):
  • WpWodify\DataTest::test_get_cache
818283848586878889909192
Covered by 2 test(s):
  • WpWodify\DataTest::test_get with data set "cache has expected result"
  • WpWodify\DataTest::test_get with data set "cache has nothing "
93
Covered by 2 test(s):
  • WpWodify\DataTest::test_get with data set "cache has expected result"
  • WpWodify\DataTest::test_get with data set "cache has nothing "
94
Covered by 2 test(s):
  • WpWodify\DataTest::test_get with data set "cache has expected result"
  • WpWodify\DataTest::test_get with data set "cache has nothing "
95
Covered by 2 test(s):
  • WpWodify\DataTest::test_get with data set "cache has expected result"
  • WpWodify\DataTest::test_get with data set "cache has nothing "
96
Covered by 2 test(s):
  • WpWodify\DataTest::test_get with data set "cache has expected result"
  • WpWodify\DataTest::test_get with data set "cache has nothing "
9798
Covered by 2 test(s):
  • WpWodify\DataTest::test_get with data set "cache has expected result"
  • WpWodify\DataTest::test_get with data set "cache has nothing "
99
Covered by 1 test(s):
  • WpWodify\DataTest::test_get with data set "cache has expected result"
100101102
Covered by 1 test(s):
  • WpWodify\DataTest::test_get with data set "cache has nothing "
103
Covered by 1 test(s):
  • WpWodify\DataTest::test_get with data set "cache has nothing "
104
Covered by 1 test(s):
  • WpWodify\DataTest::test_get with data set "cache has nothing "
105106
<?php
/**
 * Data Class
 *
 * @category    PHP
 * @package     WpWodify
 * @subpackage  Data
 * @since       File available since release 1.0.x
 * @author      Cory Collier <corycollier@corycollier.com>
 */

namespace WpWodify;

/**
 * Data Class
 *
 * Encapsulation of all Wodify Data (coaches, classes, etc)
 *
 * @category    PHP
 * @package     WpWodify
 * @subpackage  Data
 * @since       Class available since release 1.0.x
 * @author      Cory Collier <corycollier@corycollier.com>
 */
class Data
{
    /**
     * Store an instance of the API to communicate to the api with.
     *
     * @var WpWodify\Api
     */
    protected $api;

    /**
     * Stores an instance of the Cache class.
     *
     * @var WpWodify\Cache
     */
    protected $cache;

    /**
     * Setter for the api property.
     *
     * @param WpWodify\Api $api An instance of the Api.
     *
     * @return WpWodify\Data Returns $this for object-chaining.
     */
    public function set_api( Api $api ) {
        $this->api = $api;
        return $this;
    }

    /**
     * Getter for the api property.
     *
     * @return WpWodify\Api The Api instance associated with this.
     */
    public function get_api() {
        return $this->api;
    }

    /**
     * Setter for the cache property.
     *
     * @param WpWodify\Cache $cache An instance of the Cache.
     *
     * @return WpWodify\Data Returns $this for object-chaining.
     */
    public function set_cache( Cache $cache ) {
        $this->cache = $cache;
        return $this;
    }

    /**
     * Getter for the cache property.
     *
     * @return WpWodify\Cache The Cache instance associated with this.
     */
    public function get_cache() {
        return $this->cache;
    }

    /**
     * Generic data get method.
     *
     * @param string $name The name of the data to get
     * @param array $params An array of parameters to use to find data with.
     *
     * @return array The result of the finding.
     */
    public function get( $name, $params ) {
        $cache = $this->get_cache();
        $api = $this->get_api();
        $cacheId = $cache->create_identifier(array_merge($params, array(
            'api_name' => $name,
        )));

        if ($result = $cache->get($cacheId)) {
            return $result;
        }

        $result = $api->get($name, $params);
        $cache->set($cacheId, $result);
        return $result;
    }
}