Completed
Push — master ( 24abed...91ab53 )
by Rafał
15:03
created

RouteLoader::load()   C

Complexity

Conditions 17
Paths 30

Size

Total Lines 64

Duplication

Lines 13
Ratio 20.31 %

Code Coverage

Tests 5
CRAP Score 18.3387

Importance

Changes 0
Metric Value
dl 13
loc 64
ccs 5
cts 6
cp 0.8333
rs 5.2166
c 0
b 0
f 0
cc 17
nc 30
nop 4
crap 18.3387

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Content Bundle.
7
 *
8
 * Copyright 2016 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2016 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\ContentBundle\Loader;
18
19
use SWP\Bundle\ContentBundle\Model\RouteInterface;
20
use SWP\Bundle\ContentBundle\Model\RouteRepositoryInterface;
21
use SWP\Component\Common\Criteria\Criteria;
22
use SWP\Component\TemplatesSystem\Gimme\Factory\MetaFactoryInterface;
23
use SWP\Component\TemplatesSystem\Gimme\Loader\LoaderInterface;
24
use SWP\Component\TemplatesSystem\Gimme\Meta\Meta;
25
use SWP\Component\TemplatesSystem\Gimme\Meta\MetaCollection;
26
27
/**
28
 * Class RouteLoader.
29
 */
30
class RouteLoader extends PaginatedLoader implements LoaderInterface
31
{
32
    /**
33
     * @var MetaFactoryInterface
34
     */
35
    protected $metaFactory;
36
37
    /**
38
     * @var RouteRepositoryInterface
39
     */
40 111
    protected $routeRepository;
41
42 111
    /**
43 111
     * @var array
44
     */
45
    protected $supportedParameters = ['name', 'slug', 'parent'];
46
47
    /**
48
     * RouteLoader constructor.
49
     *
50
     * @param MetaFactoryInterface     $metaFactory
51
     * @param RouteRepositoryInterface $routeRepository
52
     */
53
    public function __construct(MetaFactoryInterface $metaFactory, RouteRepositoryInterface $routeRepository)
54
    {
55
        $this->metaFactory = $metaFactory;
56
        $this->routeRepository = $routeRepository;
57
    }
58
59
    /**
60
     *  {@inheritdoc}
61 11
     */
62
    public function load($type, $parameters = [], $withoutParameters = [], $responseType = LoaderInterface::SINGLE)
63 11
    {
64
        if (LoaderInterface::SINGLE === $responseType) {
65 11
            $route = $parameters['route_object'] ?? null;
66 11
            if (null === $route) {
67
                if (empty($parameters)) {
68
                    return false;
69
                }
70
71
                $criteria = new Criteria();
72
                foreach ($this->supportedParameters as $supportedParameter) {
73
                    if (array_key_exists($supportedParameter, $parameters)) {
74
                        $criteria->set($supportedParameter, $parameters[$supportedParameter]);
75
                    }
76
                }
77
78
                $route = $this->routeRepository->getQueryByCriteria($criteria, [], 'r')
79 11
                    ->setMaxResults(1)
80
                    ->getQuery()
81 11
                    ->getOneOrNullResult();
82
            }
83
84
            if (null !== $route) {
85
                return $this->metaFactory->create($route);
86
            }
87
        } elseif (LoaderInterface::COLLECTION === $responseType) {
88
            $criteria = new Criteria();
89
90
            if (\array_key_exists('parent', $parameters)) {
91
                $parent = $parameters['parent'];
92
                if (is_numeric($parent)) {
93
                    $criteria->set('parent', $parent);
94
                } elseif (\is_string($parent)) {
95
                    $parentObject = $this->routeRepository->getQueryByCriteria(new Criteria(['name' => $parent]), $criteria->get('order', []), 'r')
96
                        ->getQuery()->getOneOrNullResult();
97
                    if (null !== $parentObject) {
98
                        $criteria->set('parent', $parentObject->getId());
99
                    }
100
                } elseif ($parent instanceof Meta && $parent->getValues() instanceof RouteInterface) {
101
                    $criteria->set('parent', $parent->getValues()->getId());
102
                }
103
            }
104
105
            $this->applyPaginationToCriteria($criteria, $parameters);
106
            $countCriteria = clone $criteria;
107
            $routesCollection = $this->routeRepository->getQueryByCriteria($criteria, $criteria->get('order', []), 'r')->getQuery()->getResult();
108
109 View Code Duplication
            if (\count($routesCollection) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
                $metaCollection = new MetaCollection();
111
                $metaCollection->setTotalItemsCount($this->routeRepository->countByCriteria($countCriteria));
112
                foreach ($routesCollection as $route) {
113
                    $routeMeta = $this->metaFactory->create($route);
114
                    if (null !== $routeMeta) {
115
                        $metaCollection->add($routeMeta);
116
                    }
117
                }
118
                unset($routesCollection, $route, $criteria);
119
120
                return $metaCollection;
121
            }
122
        }
123
124
        return false;
125
    }
126
127
    /**
128
     * Checks if Loader supports provided type.
129
     *
130
     * @param string $type
131
     *
132
     * @return bool
133
     */
134
    public function isSupported(string $type): bool
135
    {
136
        return \in_array($type, ['route', 'routes']);
137
    }
138
}
139