LocalSettingProvider::populateSettings()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 2
crap 2
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\Setting;
19
use PhpMob\Settings\Model\SettingInterface;
20
use PhpMob\Settings\Schema\SettingSchema;
21
use PhpMob\Settings\Schema\SettingSchemaRegistryInterface;
22
23
/**
24
 * @author Ishmael Doss <[email protected]>
25
 */
26
class LocalSettingProvider implements SettingProviderInterface
27
{
28
    /**
29
     * @var SettingSchemaRegistryInterface
30
     */
31
    private $schemaRegistry;
32
33
    /**
34
     * @param SettingSchemaRegistryInterface $schemaRegistry
35
     */
36 4
    public function __construct(SettingSchemaRegistryInterface $schemaRegistry)
37
    {
38 4
        $this->schemaRegistry = $schemaRegistry;
39 4
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 2
    public function findUserSettings(string $owner): Collection
45
    {
46 2
        $settings = [];
47
48 2
        foreach ($this->schemaRegistry->getOwners() as $section) {
49 2
            $settings = array_merge($settings, $section->getSettings());
50
        }
51
52 2
        return new ArrayCollection($this->populateSettings($settings, $owner));
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 1
    public function findGlobalSettings(): Collection
59
    {
60 1
        $settings = [];
61
62 1
        foreach ($this->schemaRegistry->getGlobals() as $section) {
63 1
            $settings = array_merge($settings, $section->getSettings());
64
        }
65
66 1
        return new ArrayCollection($this->populateSettings($settings));
67
    }
68
69
    /**
70
     * @param SettingSchema[] $settings
71
     * @param string|null $owner
72
     *
73
     * @return SettingInterface[]
74
     */
75 3
    private function populateSettings(array $settings, ?string $owner = null): array
76
    {
77 3
        $objects = [];
78
79 3
        foreach ($settings as $setting) {
80 3
            $objects[] = $object = new Setting();
81 3
            $object->setOwner($owner);
82 3
            $object->setSection($setting->getSection()->getName());
0 ignored issues
show
Bug introduced by
Consider using $setting->getSection()->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
83 3
            $object->setKey($setting->getKey());
84 3
            $object->setValue($setting->getValue());
85
        }
86
87 3
        return $objects;
88
    }
89
}
90