CompositeSettingProvider::mergeSettings()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
crap 3
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),
0 ignored issues
show
Bug introduced by
It seems like $this->remoteProvider->findUserSettings($owner) targeting PhpMob\Settings\Provider...ace::findUserSettings() can also be of type array<integer,object<Php...odel\SettingInterface>>; however, PhpMob\Settings\Provider...ovider::mergeSettings() does only seem to accept object<Doctrine\Common\Collections\Collection>, maybe add an additional type check?

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.

Loading history...
52 2
            $this->localProvider->findUserSettings($owner)
0 ignored issues
show
Bug introduced by
It seems like $this->localProvider->findUserSettings($owner) targeting PhpMob\Settings\Provider...ace::findUserSettings() can also be of type array<integer,object<Php...odel\SettingInterface>>; however, PhpMob\Settings\Provider...ovider::mergeSettings() does only seem to accept object<Doctrine\Common\Collections\Collection>, maybe add an additional type check?

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.

Loading history...
53
        );
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 1
    public function findGlobalSettings(): Collection
60
    {
61 1
        return $this->mergeSettings(
62 1
            $this->remoteProvider->findGlobalSettings(),
0 ignored issues
show
Bug introduced by
It seems like $this->remoteProvider->findGlobalSettings() targeting PhpMob\Settings\Provider...e::findGlobalSettings() can also be of type array<integer,object<Php...odel\SettingInterface>>; however, PhpMob\Settings\Provider...ovider::mergeSettings() does only seem to accept object<Doctrine\Common\Collections\Collection>, maybe add an additional type check?

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.

Loading history...
63 1
            $this->localProvider->findGlobalSettings()
0 ignored issues
show
Bug introduced by
It seems like $this->localProvider->findGlobalSettings() targeting PhpMob\Settings\Provider...e::findGlobalSettings() can also be of type array<integer,object<Php...odel\SettingInterface>>; however, PhpMob\Settings\Provider...ovider::mergeSettings() does only seem to accept object<Doctrine\Common\Collections\Collection>, maybe add an additional type check?

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.

Loading history...
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