Failed Conditions
Push — master ( 1a5cd4...b0e5e0 )
by Florent
10:14
created

Storable   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 1
dl 0
loc 141
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setFilename() 0 7 1
A getFilename() 0 4 1
A getObject() 0 4 1
A setObject() 0 4 1
A loadObjectIfNeeded() 0 13 4
A getFileContent() 0 14 4
createNewObject() 0 1 ?
createObjectFromFileContent() 0 1 ?
A getFileModificationTime() 0 6 2
A hasFileBeenUpdated() 0 8 3
A createAndSaveObject() 0 6 1
A saveObject() 0 5 1
A delete() 0 6 2
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
     * @return string
46
     */
47
    public function getFilename()
48
    {
49
        return $this->filename;
50
    }
51
52
    /**
53
     * @return \JsonSerializable
54
     */
55
    protected function getObject()
56
    {
57
        return $this->object;
58
    }
59
60
    /**
61
     * @param \JsonSerializable $object
62
     */
63
    protected function setObject(\JsonSerializable $object)
64
    {
65
        $this->object = $object;
66
    }
67
68
    protected function loadObjectIfNeeded()
69
    {
70
        if (null !== $this->getObject() && false === $this->hasFileBeenUpdated()) {
71
            return;
72
        }
73
74
        if (null === $content = $this->getFileContent()) {
75
            $this->createAndSaveObject();
76
        } else {
77
            $this->setObject($this->createObjectFromFileContent($content));
78
            $this->file_modification_time = filemtime($this->getFilename());
79
        }
80
    }
81
82
    /**
83
     * @return array|null
84
     */
85
    protected function getFileContent()
86
    {
87
        if (file_exists($this->getFilename())) {
88
            $content = file_get_contents($this->getFilename());
89
            if (false === $content) {
90
                return;
91
            }
92
            $content = json_decode($content, true);
93
            if (!is_array($content)) {
94
                return;
95
            }
96
            return $content;
97
        }
98
    }
99
100
    /**
101
     * @return \JsonSerializable
102
     */
103
    abstract protected function createNewObject();
104
105
    /**
106
     * @param array $file_content
107
     *
108
     * @return \JsonSerializable
109
     */
110
    abstract protected function createObjectFromFileContent(array $file_content);
111
112
    /**
113
     * @return int|null
114
     */
115
    protected function getFileModificationTime()
116
    {
117
        if (file_exists($this->getFilename())) {
118
            return filemtime($this->getFilename());
119
        }
120
    }
121
122
    /**
123
     * @return bool
124
     */
125
    protected function hasFileBeenUpdated()
126
    {
127
        if (null === $this->file_modification_time || null === $this->getFileModificationTime()) {
128
            return true;
129
        }
130
131
        return $this->file_modification_time !== $this->getFileModificationTime();
132
    }
133
134
    /**
135
     *
136
     */
137
    protected function createAndSaveObject()
138
    {
139
        $object = $this->createNewObject();
140
        $this->setObject($object);
141
        $this->saveObject($object);
142
    }
143
144
    protected function saveObject(\JsonSerializable $object)
145
    {
146
        file_put_contents($this->getFilename(), json_encode($object));
147
        $this->file_modification_time = filemtime($this->getFilename());
148
    }
149
150
    public function delete()
151
    {
152
        if (file_exists($this->getFilename())) {
153
            unlink($this->getFilename());
154
        }
155
    }
156
}
157