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

ArticleCriteriaMatcher::match()   D

Complexity

Conditions 19
Paths 127

Size

Total Lines 50
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 25.2938

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 20
cts 27
cp 0.7407
rs 4.9744
c 0
b 0
f 0
cc 19
eloc 27
nc 127
nop 2
crap 25.2938

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core 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\CoreBundle\Matcher;
18
19
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
20
use SWP\Component\Common\Criteria\Criteria;
21
22
final class ArticleCriteriaMatcher implements ArticleCriteriaMatcherInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27 2
    public function match(ArticleInterface $article, Criteria $criteria)
28
    {
29 2
        if ($criteria->has('route')) {
30 1
            foreach ($criteria->get('route') as $value) {
31 1
                if ($value !== $article->getRoute()->getId()) {
32 1
                    return false;
33
                }
34
            }
35
        }
36
37 2
        if ($criteria->has('author') && is_array($criteria->get('author'))) {
38 1
            foreach ($criteria->get('author') as $key => $value) {
39 1
                if ($value !== $article->getMetadataByKey('byline')) {
40 1
                    return false;
41
                }
42
            }
43
        }
44
45 2
        if ($criteria->has('publishedBefore')) {
46
            $publishedBefore = new \DateTime($criteria->get('publishedBefore'));
47
            if ($article->getPublishedAt() > $publishedBefore) {
48
                return false;
49
            }
50
        }
51
52 2
        if ($criteria->has('publishedAfter')) {
53
            $publishedAfter = new \DateTime($criteria->get('publishedAfter'));
54
            if ($article->getPublishedAt() < $publishedAfter) {
55
                return false;
56
            }
57
        }
58
59 2
        if ($criteria->has('publishedAt')
60 2
            && (!$criteria->has('publishedBefore') || !$criteria->has('publishedAfter'))) {
61 1
            $publishedAt = new \DateTime($criteria->get('publishedAt'));
62 1
            if ($publishedAt->format('d-m-Y') !== $article->getPublishedAt()->format('d-m-Y')) {
63
                return false;
64
            }
65
        }
66
67 2
        if ($criteria->has('metadata')) {
68 2
            foreach ($criteria->get('metadata') as $key => $value) {
69 2
                if ($value !== $article->getMetadataByKey($key)) {
70 2
                    return false;
71
                }
72
            }
73
        }
74
75 2
        return true;
76
    }
77
}
78