Completed
Branch feature/pre-split (b025f2)
by Anton
03:30
created

CachedResult   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 93
rs 10
c 0
b 0
f 0
wmc 6
lcom 2
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A queryString() 0 4 1
A flushCache() 0 5 1
A jsonSerialize() 0 4 1
A __debugInfo() 0 12 2
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Database\Entities\Query;
10
11
use Spiral\Cache\StoreInterface;
12
use Spiral\Database\Helpers\QueryInterpolator;
13
14
/**
15
 * CacheResult is almost identical to QueryResult by it's functionality, but used to represent query
16
 * result stored in cache storage.
17
 */
18
class CachedResult extends ArrayResult implements \JsonSerializable
19
{
20
    /**
21
     * Limits after which no records will be dumped in __debugInfo.
22
     */
23
    const DUMP_LIMIT = 500;
24
25
    /**
26
     * @var StoreInterface
27
     */
28
    protected $store = null;
29
30
    /**
31
     * @var string
32
     */
33
    protected $key = '';
34
35
    /**
36
     * Query string (not interpolated).
37
     *
38
     * @var string
39
     */
40
    protected $queryString = '';
41
42
    /**
43
     * @var array
44
     */
45
    protected $parameters = [];
46
47
    /**
48
     * @param array          $data
49
     * @param array          $parameters
50
     * @param string         $queryString
51
     * @param string         $key
52
     * @param StoreInterface $store
53
     */
54
    public function __construct(
55
        array $data = [],
56
        array $parameters = [],
57
        string $queryString,
58
        string $key,
59
        StoreInterface $store
60
    ) {
61
        parent::__construct($data);
62
63
        $this->parameters = $parameters;
64
        $this->queryString = $queryString;
65
66
        $this->key = $key;
67
        $this->store = $store;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function queryString(): string
74
    {
75
        return QueryInterpolator::interpolate($this->queryString, $this->parameters);
76
    }
77
78
    /**
79
     * Flush results stored in CacheStore and CachedResult.
80
     */
81
    public function flushCache()
82
    {
83
        $this->store->delete($this->key);
84
        $this->key = null;
85
    }
86
87
    /**
88
     * @return array
89
     */
90
    public function jsonSerialize(): array
91
    {
92
        return $this->fetchAll();
93
    }
94
95
    /**
96
     * @return array
97
     */
98
    public function __debugInfo()
99
    {
100
        return [
101
            //Cache part
102
            'key'   => $this->key,
103
            'store' => get_class($this->store),
104
105
            'query' => $this->queryString(),
106
            'count' => $this->count(),
107
            'data'  => $this->count() > self::DUMP_LIMIT ? '[TOO MANY ROWS]' : $this->fetchAll()
108
        ];
109
    }
110
}
111