Completed
Push — master ( 7a069b...4da1f4 )
by Paweł
11s
created

ReloadableInMemoryRepository   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 84
c 0
b 0
f 0
wmc 11
lcom 1
cbo 2
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A reloadThemes() 0 5 1
A findAll() 0 6 1
A findOneByName() 0 6 2
A findOneByTitle() 0 12 3
A loadThemesIfNeeded() 0 13 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2016 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2016 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
/*
18
 * This file is part of the Sylius package.
19
 *
20
 * (c) Paweł Jędrzejewski
21
 */
22
23
namespace SWP\Bundle\CoreBundle\Theme\Repository;
24
25
use Sylius\Bundle\ThemeBundle\Loader\ThemeLoaderInterface;
26
use Sylius\Bundle\ThemeBundle\Model\ThemeInterface;
27
28
/**
29
 * @author Kamil Kokot <[email protected]>
30
 * @author Paweł Mikołajczuk <[email protected]>
31
 */
32
final class ReloadableInMemoryRepository implements ReloadableThemeRepositoryInterface
33
{
34
    /**
35
     * @var ThemeInterface[]
36
     */
37
    private $themes = [];
38
39
    /**
40
     * @var ThemeLoaderInterface
41
     */
42
    private $themeLoader;
43
44
    /**
45
     * @var bool
46
     */
47
    private $themesLoaded = false;
48
49
    /**
50
     * @param ThemeLoaderInterface $themeLoader
51
     */
52
    public function __construct(ThemeLoaderInterface $themeLoader)
53
    {
54
        $this->themeLoader = $themeLoader;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function reloadThemes(): void
61
    {
62
        $this->themesLoaded = false;
63
        $this->loadThemesIfNeeded();
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function findAll()
70
    {
71
        $this->loadThemesIfNeeded();
72
73
        return $this->themes;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function findOneByName($name)
80
    {
81
        $this->loadThemesIfNeeded();
82
83
        return isset($this->themes[$name]) ? $this->themes[$name] : null;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function findOneByTitle($title)
90
    {
91
        $this->loadThemesIfNeeded();
92
93
        foreach ($this->themes as $theme) {
94
            if ($theme->getTitle() === $title) {
95
                return $theme;
96
            }
97
        }
98
99
        return null;
100
    }
101
102
    private function loadThemesIfNeeded()
103
    {
104
        if ($this->themesLoaded) {
105
            return;
106
        }
107
108
        $themes = $this->themeLoader->load();
109
        foreach ($themes as $theme) {
110
            $this->themes[$theme->getName()] = $theme;
111
        }
112
113
        $this->themesLoaded = true;
114
    }
115
}
116