Completed
Push — master ( 530ce9...1b2ae5 )
by Paweł
23:42 queued 20:31
created

ArticleLoader   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 16
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 105
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C load() 0 65 14
A isSupported() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of the Superdesk Web Publisher Content Bundle.
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
namespace SWP\Bundle\ContentBundle\Loader;
15
16
use SWP\TemplatesSystem\Gimme\Loader\LoaderInterface;
17
use SWP\TemplatesSystem\Gimme\Meta\Meta;
18
use Symfony\Component\Yaml\Parser;
19
20
class ArticleLoader implements LoaderInterface
21
{
22
    protected $serviceContainer;
23
24
    public function __construct($serviceContainer)
25
    {
26
        $this->serviceContainer = $serviceContainer;
27
    }
28
29
    /**
30
     * Load meta object by provided type and parameters.
31
     *
32
     * @MetaLoaderDoc(
33
     *     description="Article Loader loads articles from Content Repository",
34
     *     parameters={
35
     *         contentPath="SINGLE|required content path",
36
     *         slug="SINGLE|required content slug",
37
     *         pageName="COLLECTiON|name of Page for required articles"
38
     *     }
39
     * )
40
     *
41
     * @param string $type         object type
42
     * @param array  $parameters   parameters needed to load required object type
43
     * @param int    $responseType response type: single meta (LoaderInterface::SINGLE) or collection of metas (LoaderInterface::COLLECTION)
44
     *
45
     * @return Meta|Meta[]|bool false if meta cannot be loaded, a Meta instance otherwise
46
     */
47
    public function load($type, $parameters, $responseType = LoaderInterface::SINGLE)
48
    {
49
        $dm = $this->serviceContainer->get('doctrine_phpcr.odm.document_manager');
50
        $configurationPath = $this->serviceContainer->getParameter('kernel.root_dir').'/Resources/meta/article.yml';
51
        $metadataCache = $this->serviceContainer->get('doctrine_cache.providers.main_cache');
52
53
        $article = null;
54
        if (empty($parameters)) {
55
            $parameters = [];
56
        }
57
58
        // Cache meta configuration
59
        $cacheKey = md5($configurationPath);
60
        if (!$metadataCache->contains($cacheKey)) {
61
            if (!is_readable($configurationPath)) {
62
                throw new \InvalidArgumentException('Configuration file is not readable for parser');
63
            }
64
            $yaml = new Parser();
65
            $configuration = $yaml->parse(file_get_contents($configurationPath));
66
            $metadataCache->save($cacheKey, $configuration);
67
        } else {
68
            $configuration = $metadataCache->fetch($cacheKey);
69
        }
70
71
        if ($responseType === LoaderInterface::SINGLE) {
72
            if (array_key_exists('contentPath', $parameters)) {
73
                $article = $dm->find('SWP\Bundle\ContentBundle\Document\Article', $parameters['contentPath']);
74
            } elseif (array_key_exists('article', $parameters)) {
75
                $article = $parameters['article'];
76
            } elseif (array_key_exists('slug', $parameters)) {
77
                $article = $dm->getRepository('SWP\Bundle\ContentBundle\Document\Article')
78
                    ->findOneBy(array('slug' => $parameters['slug']));
79
            }
80
81
            if (!is_null($article)) {
82
                return new Meta($configuration, $article);
83
            }
84
        } elseif ($responseType === LoaderInterface::COLLECTION) {
85
            if (array_key_exists('route', $parameters)) {
86
                $pathBuilder = $this->serviceContainer->get('swp_multi_tenancy.path_builder');
87
                $route = $dm->find(null, $pathBuilder->build(
88
                    $this->serviceContainer->getParameter(
89
                        'swp_multi_tenancy.persistence.phpcr.route_basepaths'
90
                    )[0].$parameters['route']
91
                ));
92
93
                if ($route) {
94
                    $articles = $dm->getReferrers($route, null, null, null, 'SWP\Bundle\ContentBundle\Document\Article');
95
                    $metas = [];
96
                    foreach ($articles as $article) {
97
                        if (!is_null($article)) {
98
                            $metas[] = new Meta(
99
                                $configuration,
100
                                $article
101
                            );
102
                        }
103
                    }
104
105
                    return $metas;
106
                }
107
            }
108
        }
109
110
        return false;
111
    }
112
113
    /**
114
     * Checks if Loader supports provided type.
115
     *
116
     * @param string $type
117
     *
118
     * @return bool
119
     */
120
    public function isSupported($type)
121
    {
122
        return in_array($type, array('articles', 'article'));
123
    }
124
}
125