Completed
Push — master ( 4207f0...333332 )
by Kamil
23:19 queued 01:42
created

ThemeRepository::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 12
rs 9.4285
c 3
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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
namespace Sylius\Bundle\ThemeBundle\Repository;
13
14
use Sylius\Bundle\ThemeBundle\Model\ThemeInterface;
15
use Sylius\Component\Resource\Repository\InMemoryRepository;
16
17
/**
18
 * @author Kamil Kokot <[email protected]>
19
 */
20
final class ThemeRepository extends InMemoryRepository implements ThemeRepositoryInterface
21
{
22
    /**
23
     * @param array|ThemeInterface[] $themes Can be an array of serialized themes
24
     */
25
    public function __construct(array $themes = [])
26
    {
27
        parent::__construct(ThemeInterface::class);
28
29
        foreach ($themes as $theme) {
30
            if (!$theme instanceof ThemeInterface) {
31
                $theme = unserialize($theme);
32
            }
33
34
            $this->add($theme);
35
        }
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function find($id)
42
    {
43
        return $this->findOneBySlug($id);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function findOneBySlug($slug)
50
    {
51
        return $this->findOneBy(['slug' => $slug]);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function findOneByPath($path)
58
    {
59
        /** @var ThemeInterface $theme */
60
        foreach ($this->findAll() as $theme) {
61
            if (0 === strpos($path, $theme->getPath())) {
62
                return $theme;
63
            }
64
        }
65
66
        return null;
67
    }
68
}
69