Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
StoreController
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 6
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getStore
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getStoreFromCache
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 getStoreFromDB
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 storeStoreInCache
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 clearCache
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3
4namespace BuyerKiosk\BuyerKiosk\Controllers;
5
6
7class StoreController  {
8
9    private $cache;
10    private $key;
11    public $typeNum;
12    private $expire;
13
14    public function __construct($typeNum)
15    {
16        $this->cache = new \Predis\Client($_ENV['REDIS_URL']);
17        $this->key = $typeNum."_store";
18        $this->typeNum = $typeNum;
19        $this->expire = 60*60;
20
21    }
22
23    public function getStore() {
24        if($this->getStoreFromCache() != null) {
25            return $this->getStoreFromCache();
26        } else {
27            return $this->getStoreFromDB();
28        }
29    }
30
31    private function getStoreFromCache() {
32        $store = new \Store(NULL);
33        if($this->cache->exists($this->key)) {
34            $rowArray = json_decode($this->cache->get($this->key), true);
35            if(count($rowArray) == 0) {
36                $this->cache->del($this->key);
37                return null;
38            }
39            $store->createStoreFromRowArray($rowArray);
40            return $store;
41        }
42        return null;
43    }
44
45    public function getStoreFromDB() {
46        //error_log("Store from DB");
47        $store = new \Store($this->typeNum);
48        $this->storeStoreInCache($store);
49        return $store;
50    }
51
52    private function storeStoreInCache($store) {
53        $this->cache->set($this->key, json_encode($store));
54        $this->cache->expire($this->key, $this->expire);
55        return true;
56    }
57    public function clearCache() {
58        $this->cache->del($this->key);
59    }
60}