Passed
Push — master ( 501fc3...f64e27 )
by Paweł
47:53
created

ArticleProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 13.7 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 80.95%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 10
loc 73
ccs 17
cts 21
cp 0.8095
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getRepository() 0 4 1
A getParent() 0 4 1
A getRouteArticlesQuery() 0 4 1
A getOneByCriteria() 10 10 2
A getOneById() 0 8 2
A getCountByCriteria() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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