Completed
Push — master ( a682ba...4851b3 )
by Paweł
40:50
created

ArticleRepository::getByCriteria()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 13
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 4
nop 2
crap 12
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\Doctrine\ODM\PHPCR;
18
19
use Jackalope\Query\SqlQuery;
20
use PHPCR\Query\QueryInterface;
21
use SWP\Component\Common\Criteria\Criteria;
22
use SWP\Bundle\ContentBundle\Doctrine\ArticleRepositoryInterface;
23
use SWP\Bundle\StorageBundle\Doctrine\ODM\PHPCR\DocumentRepository;
24
25
class ArticleRepository extends DocumentRepository implements ArticleRepositoryInterface
26
{
27
    /**
28
     * @param string $id
29
     *
30
     * @return object
31
     */
32
    public function findBaseNode($id)
33
    {
34
        return $this->dm->find(null, $id);
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function findOneBySlug($slug)
41
    {
42
        return $this->findOneBy(['slug' => $slug]);
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function findAllArticles()
49
    {
50
        return $this->createQueryBuilder('o')->getQuery();
51
    }
52
53
    public function getByCriteria(Criteria $criteria, array $sorting)
54
    {
55
        $routeIdentifier = $this->dm->getNodeForDocument($route)->getIdentifier();
0 ignored issues
show
Bug introduced by
The variable $route does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
56
        $query = $this->getRouteArticlesQuery($routeIdentifier, $parameters);
0 ignored issues
show
Bug introduced by
The variable $parameters does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The method getRouteArticlesQuery() does not seem to exist on object<SWP\Bundle\Conten...HPCR\ArticleRepository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
        $articles = $this->dm->getDocumentsByPhpcrQuery($query, Article::class);
58
59
        //$this->getRouteArticlesQuery($routeIdentifier, [])->execute()->getRows()->count()
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
60
61
        if (isset($parameters['limit'])) {
62
            $query->setLimit($parameters['limit']);
63
        }
64
65
        if (isset($parameters['start'])) {
66
            $query->setOffset($parameters['start']);
67
        }
68
69
        return $articles;
70
    }
71
72
    /**
73
     * @param string $identifier
74
     * @param array  $order
75
     *
76
     * @return SqlQuery
77
     *
78
     * @throws \Exception
79
     */
80
    public function getQueryForRouteArticles(string $identifier, array $order = []) : SqlQuery
81
    {
82
        $queryStr = sprintf('SELECT S.route FROM [nt:unstructured] as S WHERE S.route="%s" AND S.status="%s"', $identifier, ArticleInterface::STATUS_PUBLISHED);
83
        if (count($order) === 2) {
84
            $allowedOrders = ['ASC', 'DESC'];
85
            if (!in_array(strtoupper($order[1]), $allowedOrders)) {
86
                throw new \Exception('Order filter must have two parameters with second one asc or desc, e.g. order(id, desc)');
87
            }
88
89
            if ($order[0] === 'id') {
90
                $order[0] = 'jcr:uuid';
91
            } else {
92
                // Check that the given parameter is actually a field name of a route
93
                $metaData = $this->dm->getClassMetadata('SWP\Bundle\ContentBundle\Doctrine\ODM\PHPCR\Article');
94
                if (!in_array($order[0], $metaData->getFieldNames())) {
95
                    throw new \Exception(sprintf('Only those parameters are allowed: %s. %s was given ', implode(', ', $metaData->getFieldNames()), $order[0]));
96
                }
97
            }
98
            $queryStr .= sprintf(' ORDER BY S.%s %s', $order[0], $order[1]);
99
        }
100
101
        return $this->dm->createPhpcrQuery($queryStr, QueryInterface::JCR_SQL2);
102
    }
103
}
104