Completed
Push — master ( bf375b...cc5661 )
by Rafał
07:07
created

ArticleLoader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 81
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B load() 0 32 4
A isSupported() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of the Superdesk Web Publisher Templates System.
5
 *
6
 * Copyright 2015 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Component\TemplatesSystem\Gimme\Loader;
16
17
use SWP\Component\TemplatesSystem\Gimme\Factory\MetaFactory;
18
use SWP\Component\TemplatesSystem\Gimme\Meta\Meta;
19
use SWP\Component\TemplatesSystem\Gimme\Meta\MetaCollection;
20
use Symfony\Component\Yaml\Parser;
21
22
class ArticleLoader implements LoaderInterface
23
{
24
    /**
25
     * @var string
26
     */
27
    protected $rootDir;
28
29
    /**
30
     * @var MetaFactory
31
     */
32
    protected $metaFactory;
33
34
    /**
35
     * @param string      $rootDir     path to application root directory
36
     * @param MetaFactory $metaFactory
37
     */
38
    public function __construct($rootDir, MetaFactory $metaFactory)
39
    {
40
        $this->rootDir = $rootDir;
41
        $this->metaFactory = $metaFactory;
42
    }
43
44
    /**
45
     * Load meta object by provided type and parameters.
46
     *
47
     * @MetaLoaderDoc(
48
     *     description="Article Meta Loader provide simple way to test Loader, it will be removed when real loaders will be merged.",
49
     *     parameters={}
50
     * )
51
     *
52
     * @param string $type         object type
53
     * @param array  $parameters   parameters needed to load required object type
54
     * @param int    $responseType response type: single meta (LoaderInterface::SINGLE) or collection of metas (LoaderInterface::COLLECTION)
55
     *
56
     * @return Meta|MetaCollection false if meta cannot be loaded, a Meta instance otherwise
57
     */
58
    public function load($type, $parameters = [], $responseType = LoaderInterface::SINGLE)
59
    {
60
        if (!is_readable($this->rootDir.'/Resources/meta/article.yml')) {
61
            throw new \InvalidArgumentException('Configuration file is not readable for parser');
62
        }
63
        $parser = new Parser();
64
        $configuration = (array) $parser->parse(file_get_contents($this->rootDir.'/Resources/meta/article.yml'));
65
66
        if ($responseType === LoaderInterface::SINGLE) {
67
            return $this->metaFactory->create([
68
                'title' => 'New article',
69
                'keywords' => 'lorem, ipsum, dolor, sit, amet',
70
                'don\'t expose it' => 'this should be not exposed',
71
            ], $configuration);
72
        } elseif ($responseType === LoaderInterface::COLLECTION) {
73
            $metaCollection = new MetaCollection([
74
                $this->metaFactory->create([
75
                    'title' => 'New article 1',
76
                    'keywords' => 'lorem, ipsum, dolor, sit, amet',
77
                    'don\'t expose it' => 'this should be not exposed',
78
                ], $configuration),
79
                $this->metaFactory->create([
80
                    'title' => 'New article 2',
81
                    'keywords' => 'lorem, ipsum, dolor, sit, amet',
82
                    'don\'t expose it' => 'this should be not exposed',
83
                ], $configuration),
84
            ]);
85
            $metaCollection->setTotalItemsCount(2);
86
87
            return $metaCollection;
88
        }
89
    }
90
91
    /**
92
     * Checks if Loader supports provided type.
93
     *
94
     * @param string $type
95
     *
96
     * @return bool
97
     */
98
    public function isSupported($type)
99
    {
100
        return in_array($type, ['articles', 'article']);
101
    }
102
}
103