Completed
Push — master ( 8d3dbf...7592af )
by Paweł
62:36
created

ArticleMediaProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
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\ORM\Tools\Pagination\Paginator;
20
use SWP\Bundle\ContentBundle\Doctrine\ArticleMediaRepositoryInterface;
21
use SWP\Bundle\ContentBundle\Model\ArticleMediaInterface;
22
use SWP\Bundle\ContentBundle\Provider\AbstractProvider;
23
use SWP\Bundle\ContentBundle\Provider\ArticleMediaProviderInterface;
24
use SWP\Component\Common\Criteria\Criteria;
25
use SWP\Component\Storage\Repository\RepositoryInterface;
26
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
27
use Doctrine\Common\Collections\ArrayCollection;
28
use Doctrine\Common\Collections\Collection;
29
30
/**
31
 * ArticleMediaProvider to provide media from ORM.
32
 */
33
class ArticleMediaProvider extends AbstractProvider implements ArticleMediaProviderInterface
34
{
35
    /**
36
     * @var RepositoryInterface
37
     */
38
    private $articleMediaRepository;
39
40
    /**
41
     * ArticleProvider constructor.
42
     *
43
     * @param RepositoryInterface $articleMediaRepository
44
     */
45 109
    public function __construct(
46
        RepositoryInterface $articleMediaRepository
47
    ) {
48 109
        $this->articleMediaRepository = $articleMediaRepository;
49 109
    }
50
51
    /**
52
     * @return ArticleMediaRepositoryInterface
53
     */
54 2
    public function getRepository(): ArticleMediaRepositoryInterface
55
    {
56 2
        return $this->articleMediaRepository;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getOneById($id)
63
    {
64
        return $this->getRepository()->findOneBy(['id' => $id]);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 View Code Duplication
    public function getOneByCriteria(Criteria $criteria): ArticleMediaInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
71
    {
72
        $criteria->set('maxResults', 1);
73
        $media = $this->getRepository()->getByCriteria($criteria, [])->getQuery()->getOneOrNullResult();
74
        if (null === $media) {
75
            throw new NotFoundHttpException('Media was not found');
76
        }
77
78
        return $media;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 2
    public function getManyByCriteria(Criteria $criteria): Collection
85
    {
86 2
        $query = $this->getRepository()->getByCriteria(
87
            $criteria,
88 2
            $criteria->get('order', [])
89
        )
90 2
        ->addSelect('r')
91 2
        ->leftJoin('am.renditions', 'r')
92 2
        ->addSelect('i')
93 2
        ->leftJoin('r.image', 'i')
94 2
        ->getQuery();
95
96 2
        $paginator = new Paginator($query, $fetchJoinCollection = true);
97
98 2
        return new ArrayCollection(iterator_to_array($paginator->getIterator()));
99
    }
100
101
    public function getCountByCriteria(Criteria $criteria): int
102
    {
103
        return (int) $this->getRepository()->getByCriteria(
104
            $criteria,
105
            $criteria->get('order', [])
106
        )
107
            ->select('COUNT(am.id)')
108
            ->setFirstResult(null)
109
            ->setMaxResults(null)
110
            ->getQuery()
111
            ->getSingleScalarResult();
112
    }
113
}
114