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

ThemeLoader   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 3
c 2
b 1
f 0
lcom 1
cbo 4
dl 0
loc 48
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
transformResourceContentsToArray() 0 1 ?
A __construct() 0 5 1
A load() 0 13 2
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\Loader;
13
14
use Sylius\Bundle\ThemeBundle\Factory\ThemeFactoryInterface;
15
use Sylius\Bundle\ThemeBundle\Filesystem\Filesystem;
16
use Sylius\Bundle\ThemeBundle\Filesystem\FilesystemInterface;
17
use Symfony\Component\Config\Loader\Loader;
18
19
/**
20
 * Abstract loader for themes based on files.
21
 *
22
 * @author Kamil Kokot <[email protected]>
23
 */
24
abstract class ThemeLoader extends Loader
25
{
26
    /**
27
     * @var FilesystemInterface
28
     */
29
    private $filesystem;
30
31
    /**
32
     * @var ThemeFactoryInterface
33
     */
34
    private $themeFactory;
35
36
    /**
37
     * @param FilesystemInterface $filesystem
38
     * @param ThemeFactoryInterface $themeFactory
39
     */
40
    public function __construct(FilesystemInterface $filesystem, ThemeFactoryInterface $themeFactory)
41
    {
42
        $this->filesystem = $filesystem;
43
        $this->themeFactory = $themeFactory;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function load($resource, $type = null)
50
    {
51
        if (!$this->filesystem->exists($resource)) {
52
            throw new \InvalidArgumentException(sprintf('Given theme metadata file "%s" does not exists!', $resource));
53
        }
54
55
        $themeData = $this->transformResourceContentsToArray($this->filesystem->getFileContents($resource));
56
57
        $theme = $this->themeFactory->createFromArray($themeData);
58
        $theme->setPath(substr($resource, 0, strrpos($resource, '/')));
59
60
        return $theme;
61
    }
62
63
    /**
64
     * Returns theme data array from resource contents.
65
     *
66
     * @param string $contents
67
     *
68
     * @return array
69
     */
70
    abstract protected function transformResourceContentsToArray($contents);
71
}
72