Completed
Push — master ( c3ce72...f2565d )
by Paweł
20s
created

ArticleProvider::getOneById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
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\Provider\ORM;
18
19
use Doctrine\Common\Collections\ArrayCollection;
20
use Doctrine\Common\Collections\Collection;
21
use SWP\Component\Common\Criteria\Criteria;
22
use SWP\Bundle\ContentBundle\Doctrine\ArticleRepositoryInterface;
23
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
24
use SWP\Bundle\ContentBundle\Provider\ArticleProviderInterface;
25
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
26
27
/**
28
 * ArticleProvider to provide articles from ORM.
29
 */
30
class ArticleProvider implements ArticleProviderInterface
31
{
32
    /**
33
     * @var ArticleRepositoryInterface
34
     */
35
    private $articleRepository;
36
37
    /**
38
     * ArticleProvider constructor.
39
     *
40
     * @param ArticleRepositoryInterface $articleRepository
41 111
     */
42
    public function __construct(ArticleRepositoryInterface $articleRepository)
43
    {
44 111
        $this->articleRepository = $articleRepository;
45 111
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 2
    public function getOneById($id)
51
    {
52 2
        if (!filter_var($id, FILTER_VALIDATE_INT)) {
53
            return $this->articleRepository->findOneBySlug($id);
54
        }
55
56
        return $this->articleRepository->findOneBy(['id' => $id]);
57
    }
58 11
59
    /**
60 11
     * {@inheritdoc}
61 9
     */
62
    public function getParent($id)
63
    {
64 2
        return $this->articleRepository->find($id);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getRouteArticlesQuery(string $routeIdentifier, array $order)
71
    {
72
        return $this->articleRepository->getQueryForRouteArticles($routeIdentifier, $order);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getOneByCriteria(Criteria $criteria): ArticleInterface
79
    {
80
        // set max results to null to not break joins
81
        $criteria->set('maxResults', null);
82
        $article = $this->articleRepository->getByCriteria($criteria, [])->getQuery()->getResult();
83
        if (null === $article || 0 === count($article)) {
84
            throw new NotFoundHttpException('Article was not found');
85
        }
86 12
87
        return $article[0];
88 12
    }
89 12
90 12
    /**
91 2
     * {@inheritdoc}
92
     */
93
    public function getManyByCriteria(Criteria $criteria, array $sorting): Collection
94 11
    {
95
        $articles = $this->articleRepository->getArticlesByCriteria($criteria, $sorting)->getQuery()->getResult();
96
97
        return new ArrayCollection($articles);
98
    }
99
100 2
    /**
101
     * {@inheritdoc}
102 2
     */
103
    public function getCountByCriteria(Criteria $criteria): int
104
    {
105
        return $this->articleRepository->countByCriteria($criteria);
106
    }
107
}
108