Failed Conditions
Push — master ( b0e5e0...95e1ad )
by Florent
02:57
created

Storable::hasFileBeenUpdated()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 0
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace Jose\Object;
13
14
use Assert\Assertion;
15
16
trait Storable
17
{
18
    /**
19
     * @var \JsonSerializable
20
     */
21
    protected $object;
22
23
    /**
24
     * @var string
25
     */
26
    protected $filename;
27
28
    /**
29
     * @var int|null
30
     */
31
    protected $file_modification_time = null;
32
33
    /**
34
     * @param string $filename
35
     */
36
    public function setFilename($filename)
37
    {
38
        Assertion::string($filename, 'Invalid filename.');
39
        Assertion::directory(dirname($filename), 'The selected directory does not exist.');
40
        Assertion::writeable(dirname($filename), 'The selected directory is not writable.');
41
        $this->filename = $filename;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function regen()
48
    {
49
        $this->delete();
50
        $this->loadObjectIfNeeded();
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function delete()
57
    {
58
        if (file_exists($this->getFilename())) {
59
            unlink($this->getFilename());
60
        }
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    protected function getFilename()
67
    {
68
        return $this->filename;
69
    }
70
71
    /**
72
     * @return \JsonSerializable
73
     */
74
    protected function getObject()
75
    {
76
        return $this->object;
77
    }
78
79
    /**
80
     * @param \JsonSerializable $object
81
     */
82
    protected function setObject(\JsonSerializable $object)
83
    {
84
        $this->object = $object;
85
    }
86
87
    protected function loadObjectIfNeeded()
88
    {
89
        if (null !== $this->getObject() && false === $this->hasFileBeenUpdated()) {
90
            return;
91
        }
92
93
        if (null === $content = $this->getFileContent()) {
94
            $this->createAndSaveObject();
95
        } else {
96
            $this->setObject($this->createObjectFromFileContent($content));
97
            $this->file_modification_time = filemtime($this->getFilename());
98
        }
99
    }
100
101
    /**
102
     * @return array|null
103
     */
104
    protected function getFileContent()
105
    {
106
        if (file_exists($this->getFilename())) {
107
            $content = file_get_contents($this->getFilename());
108
            if (false === $content) {
109
                return;
110
            }
111
            $content = json_decode($content, true);
112
            if (!is_array($content)) {
113
                return;
114
            }
115
            return $content;
116
        }
117
    }
118
119
    /**
120
     * @return \JsonSerializable
121
     */
122
    abstract protected function createNewObject();
123
124
    /**
125
     * @param array $file_content
126
     *
127
     * @return \JsonSerializable
128
     */
129
    abstract protected function createObjectFromFileContent(array $file_content);
130
131
    /**
132
     * @return int|null
133
     */
134
    protected function getFileModificationTime()
135
    {
136
        if (file_exists($this->getFilename())) {
137
            return filemtime($this->getFilename());
138
        }
139
    }
140
141
    /**
142
     * @return bool
143
     */
144
    protected function hasFileBeenUpdated()
145
    {
146
        if (null === $this->file_modification_time || null === $this->getFileModificationTime()) {
147
            return true;
148
        }
149
150
        return $this->file_modification_time !== $this->getFileModificationTime();
151
    }
152
153
    /**
154
     *
155
     */
156
    protected function createAndSaveObject()
157
    {
158
        $object = $this->createNewObject();
159
        $this->setObject($object);
160
        $this->saveObject($object);
161
    }
162
163
    protected function saveObject(\JsonSerializable $object)
164
    {
165
        file_put_contents($this->getFilename(), json_encode($object));
166
        $this->file_modification_time = filemtime($this->getFilename());
167
    }
168
}
169