1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PhpMob package. |
5
|
|
|
* |
6
|
|
|
* (c) Ishmael Doss <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace PhpMob\Settings\Provider; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
17
|
|
|
use Doctrine\Common\Collections\Collection; |
18
|
|
|
use PhpMob\Settings\Model\SettingInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Ishmael Doss <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class CompositeSettingProvider implements SettingProviderInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var SettingProviderInterface |
27
|
|
|
*/ |
28
|
|
|
private $localProvider; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var SettingProviderInterface |
32
|
|
|
*/ |
33
|
|
|
private $remoteProvider; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param SettingProviderInterface $localProvider |
37
|
|
|
* @param SettingProviderInterface $remoteProvider |
38
|
|
|
*/ |
39
|
4 |
|
public function __construct(SettingProviderInterface $localProvider, SettingProviderInterface $remoteProvider) |
40
|
|
|
{ |
41
|
4 |
|
$this->localProvider = $localProvider; |
42
|
4 |
|
$this->remoteProvider = $remoteProvider; |
43
|
4 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
2 |
|
public function findUserSettings(string $owner): Collection |
49
|
|
|
{ |
50
|
2 |
|
return $this->mergeSettings( |
51
|
2 |
|
$this->remoteProvider->findUserSettings($owner), |
|
|
|
|
52
|
2 |
|
$this->localProvider->findUserSettings($owner) |
|
|
|
|
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
1 |
|
public function findGlobalSettings(): Collection |
60
|
|
|
{ |
61
|
1 |
|
return $this->mergeSettings( |
62
|
1 |
|
$this->remoteProvider->findGlobalSettings(), |
|
|
|
|
63
|
1 |
|
$this->localProvider->findGlobalSettings() |
|
|
|
|
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param Collection $remotes |
69
|
|
|
* @param Collection $locals |
70
|
|
|
* |
71
|
|
|
* @return ArrayCollection |
72
|
|
|
*/ |
73
|
3 |
|
private function mergeSettings(Collection $remotes, Collection $locals): ArrayCollection |
74
|
|
|
{ |
75
|
3 |
|
$settings = $remotes->toArray(); |
76
|
|
|
|
77
|
|
|
/** @var SettingInterface $local */ |
78
|
3 |
|
foreach ($locals->toArray() as $local) { |
79
|
3 |
|
if (!$this->exists($remotes, $local)) { |
80
|
3 |
|
$settings[] = $local; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
return new ArrayCollection($settings); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param Collection $remote |
89
|
|
|
* @param SettingInterface $object |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
private function exists(Collection $remote, SettingInterface $object): bool |
94
|
|
|
{ |
95
|
3 |
|
return $remote->exists(function (int $key, SettingInterface $setting) use ($object) { |
96
|
3 |
|
return $setting->isEqual($object); |
97
|
3 |
|
}); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.