ArticleLoader   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A load() 0 34 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\MetaCollection;
19
use Symfony\Component\Yaml\Parser;
20
21
class ArticleLoader implements LoaderInterface
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $rootDir;
27
28
    /**
29
     * @var MetaFactory
30
     */
31
    protected $metaFactory;
32
33
    /**
34
     * @param string      $rootDir     path to application root directory
35
     * @param MetaFactory $metaFactory
36
     */
37
    public function __construct($rootDir, MetaFactory $metaFactory)
38
    {
39
        $this->rootDir = $rootDir;
40
        $this->metaFactory = $metaFactory;
41
    }
42
43
    /**
44
     *  {@inheritdoc}
45
     */
46
    public function load($type, $parameters = [], $withoutParameters = [], $responseType = LoaderInterface::SINGLE)
47
    {
48
        if (!is_readable($this->rootDir.'/Resources/meta/article.yml')) {
49
            throw new \InvalidArgumentException('Configuration file is not readable for parser');
50
        }
51
        $parser = new Parser();
52
        $configuration = (array) $parser->parse(file_get_contents($this->rootDir.'/Resources/meta/article.yml'));
53
54
        if (LoaderInterface::SINGLE === $responseType) {
55
            return $this->metaFactory->create([
56
                'title' => 'New article',
57
                'keywords' => 'lorem, ipsum, dolor, sit, amet',
58
                'don\'t expose it' => 'this should be not exposed',
59
            ], $configuration);
60
        } elseif (LoaderInterface::COLLECTION === $responseType) {
61
            $metaCollection = new MetaCollection([
62
                $this->metaFactory->create([
63
                    'title' => 'New article 1',
64
                    'keywords' => 'lorem, ipsum, dolor, sit, amet',
65
                    'don\'t expose it' => 'this should be not exposed',
66
                ], $configuration),
67
                $this->metaFactory->create([
68
                    'title' => 'New article 2',
69
                    'keywords' => 'lorem, ipsum, dolor, sit, amet',
70
                    'don\'t expose it' => 'this should be not exposed',
71
                ], $configuration),
72
            ]);
73
            $metaCollection->setTotalItemsCount(2);
74
75
            return $metaCollection;
76
        }
77
78
        return false;
79
    }
80
81
    /**
82
     * Checks if Loader supports provided type.
83
     *
84
     * @param string $type
85
     *
86
     * @return bool
87
     */
88
    public function isSupported(string $type): bool
89
    {
90
        return in_array($type, ['articles', 'article']);
91
    }
92
}
93