Result   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 114
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getSource() 0 4 1
A setSource() 0 5 1
A getCacheKey() 0 4 1
A setCacheKey() 0 5 1
A getFile() 0 4 1
A setFile() 0 6 1
A setCached() 0 5 1
A isCached() 0 4 1
A import() 0 16 2
A export() 0 12 1
1
<?php
2
/**
3
 *
4
 * PHP version 5.5
5
 *
6
 * @package TTS
7
 * @author  Sergey V.Kuzin <[email protected]>
8
 * @license MIT
9
 */
10
11
namespace TTS;
12
13
14
class Result
15
{
16
    protected $source;
17
18
    protected $cacheKey;
19
20
    protected $file;
21
22
    /** @var \DateTime */
23
    protected $createdAt;
24
25
    /** @var \DateTime */
26
    protected $lastAccess;
27
28
    protected $count = 0;
29
30
    protected $cached = false;
31
32
    /**
33
     * @return mixed
34
     */
35
    public function getSource()
36
    {
37
        return $this->source;
38
    }
39
40
    /**
41
     * @param mixed $source
42
     */
43
    public function setSource($source)
44
    {
45
        $this->source = $source;
46
        return $this;
47
    }
48
49
    /**
50
     * @return mixed
51
     */
52
    public function getCacheKey()
53
    {
54
        return $this->cacheKey;
55
    }
56
57
    /**
58
     * @param mixed $cacheKey
59
     */
60
    public function setCacheKey($cacheKey)
61
    {
62
        $this->cacheKey = $cacheKey;
63
        return $this;
64
    }
65
66
    /**
67
     * @return mixed
68
     */
69
    public function getFile()
70
    {
71
        return $this->file;
72
    }
73
74
    /**
75
     * @param mixed $file
76
     */
77
    public function setFile($file)
78
    {
79
        $this->file = $file;
80
        $this->import();
81
        return $this;
82
    }
83
84
    public function setCached($cached = true)
85
    {
86
        $this->cached = (bool)$cached;
87
        return $this;
88
    }
89
90
    public function isCached()
91
    {
92
        return $this->cached;
93
    }
94
95
    public function import()
96
    {
97
        $file = $this->file . '.json';
98
99
        if (file_exists($file)) {
100
            $data = json_decode(
101
                file_get_contents($file),
102
                true
103
            );
104
            $this->createdAt = new \DateTime($data['createdAt']);
105
            $this->count = $data['count'];
106
        } else {
107
            $this->createdAt = new \DateTime();
108
            $this->count = 1;
109
        }
110
    }
111
112
    /**
113
     *
114
     */
115
    public function export()
116
    {
117
        $file = $this->file . '.json';
118
119
        file_put_contents($file, json_encode([
120
            'createdAt' => $this->createdAt->format(\DateTime::RFC3339),
121
            'source' => $this->source,
122
            'count' => ++$this->count,
123
            'lastAccess' => date(\DateTime::RFC3339)
124
        ], JSON_UNESCAPED_UNICODE));
125
126
    }
127
}
128