Completed
Push — master ( f576a1...b86af8 )
by Mārtiņš
01:55
created

MessageRevisions::writeToFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Printful\GettextCms;
4
5
use Gettext\Translation;
6
use Gettext\Translations;
7
use Printful\GettextCms\Exceptions\InvalidPathException;
8
use Printful\GettextCms\Interfaces\MessageConfigInterface;
9
use Printful\GettextCms\Structures\RevisionItem;
10
11
class MessageRevisions
12
{
13
    /** @var MessageConfigInterface */
14
    private $config;
15
16
    /**
17
     * Cached instance so we don't read the file each time
18
     * @var RevisionItem
19
     */
20
    private $revisionCache = null;
21
22 13
    public function __construct(MessageConfigInterface $config)
23
    {
24 13
        $this->config = $config;
25 13
    }
26
27
    /**
28
     * Get revisioned domain or the original domain name if revision does not exist for this domain
29
     *
30
     * @param string $locale
31
     * @param string $domain
32
     * @return string
33
     */
34 5
    public function getRevisionedDomain(string $locale, string $domain): string
35
    {
36 5
        if (!$this->revisionCache) {
37 5
            $this->revisionCache = $this->readFromFile();
38
        }
39
40 5
        $revisionedDomain = $this->revisionCache->getRevisionedDomain($locale, $domain);
41
42 5
        return $revisionedDomain ?: $domain;
43
    }
44
45
    /**
46
     * From given translations, create a versioned domain name "domain_XXXXXX"
47
     *
48
     * @param string $domain
49
     * @param Translations $translations
50
     * @return string
51
     */
52 5
    public function generateRevisionedDomain(string $domain, Translations $translations): string
53
    {
54 5
        return $domain . '-' . $this->generateHash($translations);
55
    }
56
57
    private function generateHash(Translations $translations): string
58
    {
59 5
        $array = array_map(function (Translation $t) {
60
            return [
61 5
                $t->getOriginal(),
62 5
                $t->getPlural(),
63 5
                $t->getTranslation(),
64 5
                $t->getPluralTranslations(),
65 5
                $t->getContext(),
66
            ];
67 5
        }, (array)$translations);
68
69 5
        return substr(md5(serialize($array)), 0, 6); // Take only 6, and pray for no collisions
70
    }
71
72
    /**
73
     * Store in revision file that a new revision was created
74
     *
75
     * @param string $locale
76
     * @param string $originalDomain
77
     * @param string $revisionedDomain
78
     * @return bool
79
     * @throws InvalidPathException
80
     */
81 6
    public function saveRevision(string $locale, string $originalDomain, string $revisionedDomain): bool
82
    {
83 6
        $revisions = $this->readFromFile();
84
85 6
        $previousRevision = $revisions->getRevisionedDomain($locale, $originalDomain);
86
87 6
        if ($previousRevision === $revisionedDomain) {
88
            // No need to save, because it's the same revision
89 1
            return true;
90
        }
91
92 6
        $revisions->setRevisionedDomain($locale, $originalDomain, $revisionedDomain);
93
94 6
        return $this->writeToFile($revisions);
95
    }
96
97
    /**
98
     * Read revision data from file
99
     *
100
     * @return RevisionItem
101
     */
102 6
    private function readFromFile(): RevisionItem
103
    {
104 6
        $pathname = $this->getPathname();
105
106 6
        if (is_file($pathname)) {
107 4
            return RevisionItem::fromArray(json_decode(file_get_contents($pathname), true));
108
        }
109
110 6
        return new RevisionItem;
111
    }
112
113
    /**
114
     * Write revision data to json file
115
     *
116
     * @param Structures\RevisionItem $revisions
117
     * @return bool
118
     * @throws InvalidPathException
119
     */
120 6
    private function writeToFile(RevisionItem $revisions): bool
121
    {
122
        // Update the cached version
123 6
        $this->revisionCache = $revisions;
124
125 6
        $pathname = $this->getPathname();
126
127 6
        if (!is_dir(dirname($pathname))) {
128 1
            throw new InvalidPathException('Directory does not exist (' . $this->config->getMoDirectory() . ')');
129
        }
130
131 5
        return file_put_contents($pathname, json_encode($revisions->toArray(), JSON_PRETTY_PRINT));
0 ignored issues
show
Bug Best Practice introduced by
The expression return file_put_contents...Cms\JSON_PRETTY_PRINT)) returns the type integer which is incompatible with the type-hinted return boolean.
Loading history...
132
    }
133
134
    /**
135
     * Full path to revision file
136
     *
137
     * @return string
138
     */
139 6
    private function getPathname(): string
140
    {
141 6
        return rtrim($this->config->getMoDirectory(), '/') . '/' . 'revisions.json';
142
    }
143
}