Completed
Push — master ( bcc053...84e7ff )
by Mārtiņš
01:39
created

MessageArrayRepository   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 19
eloc 36
dl 0
loc 121
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setAllAsNotDynamic() 0 8 3
A getRequiresTranslating() 0 4 1
A getEnabledTranslated() 0 6 1
A getSingle() 0 9 3
A getEnabled() 0 6 1
A getEnabledTranslatedJs() 0 6 1
A setAllAsNotInFilesAndInJs() 0 9 3
A save() 0 4 1
A disableUnused() 0 6 3
A getAll() 0 4 2
1
<?php
2
3
namespace Printful\GettextCms;
4
5
use Printful\GettextCms\Interfaces\MessageRepositoryInterface;
6
use Printful\GettextCms\Structures\MessageItem;
7
8
class MessageArrayRepository implements MessageRepositoryInterface
9
{
10
    /** @var MessageItem[] */
11
    private $store = [];
12
13
    /**
14
     * @inheritdoc
15
     */
16
    public function save(MessageItem $item): bool
17
    {
18
        $this->store[$item->key] = $item;
19
        return true;
20
    }
21
22
    /**
23
     * @inheritdoc
24
     */
25
    public function getAll(string $locale, string $domain): array
26
    {
27
        return array_filter($this->store, function (MessageItem $item) use ($locale, $domain) {
28
            return $item->locale === $locale && $item->domain === $domain;
29
        });
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function getEnabled(string $locale, string $domain): array
36
    {
37
        $allItems = $this->getAll($locale, $domain);
38
39
        return array_filter($allItems, function (MessageItem $item) {
40
            return !$item->isDisabled;
41
        });
42
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function getEnabledTranslated(string $locale, string $domain): array
49
    {
50
        $enabledItems = $this->getEnabled($locale, $domain);
51
52
        return array_filter($enabledItems, function (MessageItem $item) {
53
            return $item->hasOriginalTranslation;
54
        });
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function getEnabledTranslatedJs(string $locale, string $domain): array
61
    {
62
        $enabledTranslated = $this->getEnabledTranslated($locale, $domain);
63
64
        return array_filter($enabledTranslated, function (MessageItem $item) {
65
            return $item->isInJs;
66
        });
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public function getSingle($key): MessageItem
73
    {
74
        foreach ($this->store as $v) {
75
            if ($v->key === $key) {
76
                return $v;
77
            }
78
        }
79
80
        return new MessageItem();
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86
    public function setAllAsNotInFilesAndInJs(string $locale, string $domain)
87
    {
88
        $this->store = array_map(function (MessageItem $item) use ($locale, $domain) {
89
            if ($item->domain === $domain && $item->locale === $locale) {
90
                $item->isInFile = false;
91
                $item->isInJs = false;
92
            }
93
            return $item;
94
        }, $this->store);
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100
    public function setAllAsNotDynamic(string $locale, string $domain)
101
    {
102
        $this->store = array_map(function (MessageItem $item) use ($locale, $domain) {
103
            if ($item->domain === $domain && $item->locale === $locale) {
104
                $item->isDynamic = false;
105
            }
106
            return $item;
107
        }, $this->store);
108
    }
109
110
    /**
111
     * @inheritdoc
112
     */
113
    public function getRequiresTranslating(string $locale, string $domain): array
114
    {
115
        return array_filter($this->getEnabled($locale, $domain), function (MessageItem $item) {
116
            return $item->requiresTranslating;
117
        });
118
    }
119
120
    /**
121
     * @inheritdoc
122
     */
123
    public function disableUnused()
124
    {
125
        $this->store = array_map(function (MessageItem $item) {
126
            $item->isDisabled = !$item->isDynamic && !$item->isInJs && !$item->isInFile;
127
            return $item;
128
        }, $this->store);
129
    }
130
}