Test Failed
Push — develop ( 9dccb9...240a5e )
by Nuno
03:56
created

Status::remoteGotUpdated()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Scout Extended.
7
 *
8
 * (c) Algolia Team <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Algolia\ScoutExtended\Settings;
15
16
use Algolia\AlgoliaSearch\Index;
0 ignored issues
show
Bug introduced by
The type Algolia\AlgoliaSearch\Index was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use LogicException;
18
use Illuminate\Support\Str;
19
use Illuminate\Filesystem\Filesystem;
20
21
/**
22
 * @internal
23
 */
24
final class Status
25
{
26
    /**
27
     * @var \Algolia\ScoutExtended\Settings\Encrypter
28
     */
29
    private $encrypter;
30
31
    /**
32
     * @var \Algolia\ScoutExtended\Settings\LocalRepository
33
     */
34
    private $localRepository;
35
36
    /**
37
     * @var \Algolia\ScoutExtended\Settings\Settings
38
     */
39
    private $remoteSettings;
40
41
    /**
42
     * @var \Algolia\AlgoliaSearch\Index
43
     */
44
    private $index;
45
46
    public const LOCAL_NOT_FOUND = 'localNotFound';
47
48
    public const  REMOTE_NOT_FOUND = 'remoteNotFound';
49
50
    public const  BOTH_ARE_EQUAL = 'bothAreEqual';
51
52
    public const  LOCAL_GOT_UPDATED = 'localGotUpdated';
53
54
    public const  REMOTE_GOT_UPDATED = 'remoteGotUpdated';
55
56
    public const  BOTH_GOT_UPDATED = 'bothGotUpdated';
57
58
    /**
59
     * Status constructor.
60
     *
61
     * @param \Algolia\ScoutExtended\Settings\LocalRepository $localRepository
62
     * @param \Algolia\ScoutExtended\Settings\Encrypter $encrypter
63
     * @param \Algolia\ScoutExtended\Settings\Settings $remoteSettings
64
     * @param \Algolia\AlgoliaSearch\Index $index
65
     *
66
     * @return void
67
     */
68
    public function __construct(
69
        LocalRepository $localRepository,
70
        Encrypter $encrypter,
71
        Settings $remoteSettings,
72
        Index $index
73
    ) {
74
        $this->encrypter = $encrypter;
75
        $this->localRepository = $localRepository;
76
        $this->remoteSettings = $remoteSettings;
77
        $this->index = $index;
78
    }
79
80
    /**
81
     * @return bool
82
     */
83
    public function localNotFound(): bool
84
    {
85
        return ! $this->localRepository->exists($this->index);
86
    }
87
88
    /**
89
     * @return bool
90
     */
91
    public function remoteNotFound(): bool
92
    {
93
        return empty($this->remoteSettings->previousHash());
94
    }
95
96
    /**
97
     * @return bool
98
     */
99
    public function bothAreEqual(): bool
100
    {
101
        return $this->encrypter->encrypt($this->localRepository->find($this->index)) === $this->remoteSettings->previousHash() && $this->encrypter->encrypt($this->remoteSettings) === $this->remoteSettings->previousHash();
102
    }
103
104
    /**
105
     * @return bool
106
     */
107
    public function localGotUpdated(): bool
108
    {
109
        return $this->encrypter->encrypt($this->localRepository->find($this->index)) !== $this->remoteSettings->previousHash() && $this->encrypter->encrypt($this->remoteSettings) === $this->remoteSettings->previousHash();
110
    }
111
112
    /**
113
     * @return bool
114
     */
115
    public function remoteGotUpdated(): bool
116
    {
117
        return $this->encrypter->encrypt($this->localRepository->find($this->index)) === $this->remoteSettings->previousHash() && $this->encrypter->encrypt($this->remoteSettings) !== $this->remoteSettings->previousHash();
118
    }
119
120
    /**
121
     * @return bool
122
     */
123
    public function bothGotUpdated(): bool
124
    {
125
        return $this->encrypter->encrypt($this->localRepository->find($this->index)) !== $this->remoteSettings->previousHash() && $this->encrypter->encrypt($this->remoteSettings) !== $this->remoteSettings->previousHash();
126
    }
127
128
    /**
129
     * Get the current state.
130
     *
131
     * @return string
132
     */
133
    public function toString(): string
134
    {
135
        $methods = [
136
            self::LOCAL_NOT_FOUND,
137
            self::REMOTE_NOT_FOUND,
138
            self::BOTH_ARE_EQUAL,
139
            self::LOCAL_GOT_UPDATED,
140
            self::REMOTE_GOT_UPDATED,
141
            self::BOTH_GOT_UPDATED,
142
        ];
143
144
        foreach ($methods as $method) {
145
            if ($this->{$method}()) {
146
                return $method;
147
            }
148
        }
149
150
        throw new LogicException('This should not happen');
151
    }
152
153
    /**
154
     * Get a human description of the current status.
155
     *
156
     * @return string
157
     */
158
    public function toHumanString(): string
159
    {
160
        $string = Str::snake($this->toString());
161
162
        return Str::ucfirst(str_replace('_', ' ', $string));
163
    }
164
}
165