Completed
Push — theme-bundle ( 2fd15f...927586 )
by Kamil
26:43 queued 10s
created

ThemeHierarchyProvider::getTheme()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 10
rs 9.4286
cc 2
eloc 5
nc 2
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\HierarchyProvider;
13
14
use Sylius\Bundle\ThemeBundle\Model\ThemeInterface;
15
use Sylius\Bundle\ThemeBundle\Repository\ThemeRepositoryInterface;
16
17
/**
18
 * @author Kamil Kokot <[email protected]>
19
 */
20
final class ThemeHierarchyProvider implements ThemeHierarchyProviderInterface
21
{
22
    /**
23
     * @var ThemeRepositoryInterface
24
     */
25
    private $themeRepository;
26
27
    /**
28
     * @param ThemeRepositoryInterface $themeRepository
29
     */
30
    public function __construct(ThemeRepositoryInterface $themeRepository)
31
    {
32
        $this->themeRepository = $themeRepository;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getThemeHierarchy(ThemeInterface $theme)
39
    {
40
        $parents = [];
41
        $parentsSlugs = $theme->getParentsSlugs();
42
        foreach ($parentsSlugs as $parentName) {
43
            $parents = array_merge(
44
                $parents,
45
                $this->getThemeHierarchy($this->getTheme($parentName))
46
            );
47
        }
48
49
        return array_merge([$theme], $parents);
50
    }
51
52
    /**
53
     * @param string $themeName
54
     *
55
     * @return ThemeInterface
56
     *
57
     * @throws \InvalidArgumentException If theme is not found
58
     */
59
    private function getTheme($themeName)
60
    {
61
        $theme = $this->themeRepository->findOneBySlug($themeName);
62
63
        if (null === $theme) {
64
            throw new \InvalidArgumentException(sprintf('Theme "%s" not found!', $themeName));
65
        }
66
67
        return $theme;
68
    }
69
}
70