Completed
Pull Request — master (#7)
by Paweł
16:44
created

ArticleLoader::load()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 29
rs 8.5806
cc 4
eloc 20
nc 4
nop 3
1
<?php
2
3
/**
4
 * This file is part of the Superdesk Web Publisher Templates System
5
 *
6
 * Copyright 2015 Sourcefabric z.u. 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\TemplatesSystem\Gimme\Loader;
16
17
use SWP\TemplatesSystem\Gimme\Meta\Meta;
18
use Symfony\Component\Yaml\Parser;
19
20
class ArticleLoader implements LoaderInterface
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $rootDir;
26
27
    /**
28
     * @param string $rootDir path to application root directory
29
     */
30
    public function __construct($rootDir)
31
    {
32
        $this->rootDir = $rootDir;
33
    }
34
35
    /**
36
     * Load meta object by provided type and parameters
37
     *
38
     * @MetaLoaderDoc(
39
     *     description="Article Meta Loader provide simple way to test Loader, it will be removed when real loaders will be merged.",
40
     *     parameters={}
41
     * )
42
     *
43
     * @param string $type         object type
44
     * @param array  $parameters   parameters needed to load required object type
45
     * @param int    $responseType response type: single meta (LoaderInterface::SINGLE) or collection of metas (LoaderInterface::COLLECTION)
46
     *
47
     * @return Meta|bool false if meta cannot be loaded, a Meta instance otherwise
48
     */
49
    public function load($type, $parameters, $responseType)
50
    {
51
        if (!is_readable($this->rootDir.'/Resources/meta/article.yml')) {
52
            throw new \InvalidArgumentException("Configuration file is not readable for parser");
53
        }
54
        $yaml = new Parser();
55
        $configuration = (array) $yaml->parse(file_get_contents($this->rootDir.'/Resources/meta/article.yml'));
56
57
        if ($responseType === LoaderInterface::SINGLE) {
58
            return new Meta($configuration, array(
59
                'title' => 'New article',
60
                'keywords' => 'lorem, ipsum, dolor, sit, amet',
61
                'don\'t expose it' => 'this should be not exposed',
62
            ));
63
        } else if ($responseType === LoaderInterface::COLLECTION) {
64
            return array(
65
                new Meta($configuration, array(
66
                    'title' => 'New article 1',
67
                    'keywords' => 'lorem, ipsum, dolor, sit, amet',
68
                    'don\'t expose it' => 'this should be not exposed',
69
                )),
70
                new Meta($configuration, array(
71
                    'title' => 'New article 2',
72
                    'keywords' => 'lorem, ipsum, dolor, sit, amet',
73
                    'don\'t expose it' => 'this should be not exposed',
74
                ))
75
            );
76
        }
77
    }
78
79
    /**
80
     * Checks if Loader supports provided type
81
     *
82
     * @param string $type
83
     *
84
     * @return boolean
85
     */
86
    public function isSupported($type)
87
    {
88
        return in_array($type, array('articles', 'article'));
89
    }
90
}
91