Completed
Push — master ( 6cd8ca...cbb2b9 )
by Paweł
63:35
created

ArticleMediaProvider::getFromInternalCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 6
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 Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
26
use Doctrine\Common\Collections\ArrayCollection;
27
use Doctrine\Common\Collections\Collection;
28
29
/**
30
 * ArticleMediaProvider to provide media from ORM.
31
 */
32
class ArticleMediaProvider extends AbstractProvider implements ArticleMediaProviderInterface
33
{
34
    /**
35
     * @var ArticleMediaRepositoryInterface
36
     */
37
    private $articleMediaRepository;
38
39
    /**
40
     * @var array
41
     */
42
    private $internalCache = [];
43
44
    /**
45 109
     * ArticleMediaProvider constructor.
46
     *
47
     * @param ArticleMediaRepositoryInterface $articleMediaRepository
48 109
     */
49 109
    public function __construct(
50
        ArticleMediaRepositoryInterface $articleMediaRepository
51
    ) {
52
        $this->articleMediaRepository = $articleMediaRepository;
53
    }
54 2
55
    /**
56 2
     * @return ArticleMediaRepositoryInterface
57
     */
58
    public function getRepository(): ArticleMediaRepositoryInterface
59
    {
60
        return $this->articleMediaRepository;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getOneById($id)
67
    {
68
        return $this->getRepository()->findOneBy(['id' => $id]);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getOneByCriteria(Criteria $criteria): ArticleMediaInterface
75
    {
76
        $criteria->set('maxResults', 1);
77
        if (null !== $result = $this->getFromInternalCache($criteria)) {
78
            return $result;
79
        }
80
81
        $media = $this->getRepository()->getByCriteria($criteria, [])->getQuery()->getOneOrNullResult();
82
        if (null === $media) {
83
            throw new NotFoundHttpException('Media was not found');
84 2
        }
85
        $this->addToInternalCache($criteria, $media);
86 2
87
        return $media;
88 2
    }
89
90 2
    /**
91 2
     * {@inheritdoc}
92 2
     */
93 2
    public function getManyByCriteria(Criteria $criteria): Collection
94 2
    {
95
        if (null !== $result = $this->getFromInternalCache($criteria)) {
96 2
            return $result;
97
        }
98 2
99
        $query = $this->getRepository()->getByCriteria(
100
            $criteria,
101
            $criteria->get('order', [])
102
        )
103
        ->addSelect('r')
104
        ->leftJoin('am.renditions', 'r')
105
        ->addSelect('i')
106
        ->leftJoin('r.image', 'i')
107
        ->getQuery();
108
109
        $paginator = new Paginator($query, $fetchJoinCollection = true);
110
111
        $result = new ArrayCollection(iterator_to_array($paginator->getIterator()));
112
        $this->addToInternalCache($criteria, $result);
113
114
        return $result;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function getCountByCriteria(Criteria $criteria): int
121
    {
122
        return (int) $this->getRepository()->getByCriteria(
123
            $criteria,
124
            $criteria->get('order', [])
125
        )
126
            ->select('COUNT(am.id)')
127
            ->setFirstResult(null)
128
            ->setMaxResults(null)
129
            ->getQuery()
130
            ->getSingleScalarResult();
131
    }
132
133
    private function getFromInternalCache(Criteria $criteria)
134
    {
135
        $key = $this->getKeyForCriteria($criteria);
136
        if (array_key_exists($key, $this->internalCache)) {
137
            return $this->internalCache[$key];
138
        }
139
140
        return;
141
    }
142
143
    private function addToInternalCache(Criteria $criteria, $value)
144
    {
145
        $key = $this->getKeyForCriteria($criteria);
146
        $this->internalCache[$key] = $value;
147
    }
148
149
    private function getKeyForCriteria(Criteria $criteria): string
150
    {
151
        return md5(serialize($criteria->all()));
152
    }
153
}
154