Completed
Push — master ( 25e5a7...6469d9 )
by Paweł
62:31
created

ArticleProvider::getRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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