Completed
Push — master ( 124fed...6caa0a )
by Tomáš
12s
created

ProxySourceTaxonomyIndexGenerator::generate()   C

Complexity

Conditions 11
Paths 97

Size

Total Lines 59
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 0
cts 43
cp 0
rs 6.3545
c 0
b 0
f 0
cc 11
eloc 33
nc 97
nop 1
crap 132

How to fix   Long Method    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
/*
4
 * This file is a part of Sculpin.
5
 *
6
 * (c) Dragonfly Development Inc.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symplify\PHP7_Sculpin\PostsBundle\Taxonomy;
13
14
use Symplify\PHP7_Sculpin\DataProvider\DataProviderManager;
15
use Symplify\PHP7_Sculpin\Generator\GeneratorInterface;
16
use Symplify\PHP7_Sculpin\Source\SourceInterface;
17
18
final class ProxySourceTaxonomyIndexGenerator implements GeneratorInterface
19
{
20
    /**
21
     * @var DataProviderManager
22
     */
23
    private $dataProviderManager;
24
25
    /**
26
     * @var string
27
     */
28
    private $dataProviderName;
29
30
    /**
31
     * @var string
32
     */
33
    private $injectedTaxonKey;
34
35
    /**
36
     * @var string
37
     */
38
    private $injectedTaxonItemsKey;
39
40
    /**
41
     * @var PermalinkStrategyCollection
42
     */
43
    private $permalinkStrategyCollection;
44
45
    public function __construct(
46
        DataProviderManager $dataProviderManager,
47
        string $dataProviderName,
48
        string $injectedTaxonKey,
49
        string $injectedTaxonItemsKey,
50
        PermalinkStrategyCollection $permalinkStrategyCollection
51
    ) {
52
        $this->dataProviderManager = $dataProviderManager;
53
        $this->dataProviderName = $dataProviderName; // post_tags
54
        $this->injectedTaxonKey = $injectedTaxonKey; // tag
55
        $this->injectedTaxonItemsKey = $injectedTaxonItemsKey; // tagged_posts
56
        $this->permalinkStrategyCollection = $permalinkStrategyCollection;
57
    }
58
59
    public function generate(SourceInterface $source) : array
60
    {
61
        $dataProvider = $this->dataProviderManager->dataProvider($this->dataProviderName);
62
        $taxons = $dataProvider->provideData();
63
64
        $generatedSources = [];
65
        foreach ($taxons as $taxon => $items) {
66
            $generatedSource = $source->duplicate(
67
                $source->sourceId().':'.$this->injectedTaxonKey.'='.$taxon
68
            );
69
70
            $permalink = $source->data()->get('permalink') ?: $source->relativePathname();
71
            $basename = basename($permalink);
72
73
            $permalink = dirname($permalink);
74
75
            $indexType = null;
76
77
            if (preg_match('/^(.+?)\.(.+)$/', $basename, $matches)) {
78
                $urlTaxon = $this->permalinkStrategyCollection->process($taxon);
79
                $indexType = $matches[2];
80
                $suffix = in_array($indexType, ['xml', 'rss', 'json']) ? '.'.$indexType : '/';
81
                $permalink = $permalink.'/'.$urlTaxon.$suffix;
82
            } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
83
                // not sure what case this is?
84
            }
85
86
            if (0 === strpos($permalink, './')) {
87
                $permalink = substr($permalink, 2);
88
            }
89
90
            if (0 !== strpos($permalink, '/')) {
91
                $permalink = '/'.$permalink;
92
            }
93
94
            if ($permalink) {
95
                // not sure if this is ever going to happen?
96
                $generatedSource->data()->set('permalink', $permalink);
97
            }
98
99
            $generatedSource->data()->set($this->injectedTaxonKey, $taxon);
100
            $generatedSource->data()->set($this->injectedTaxonItemsKey, $items);
101
102
            if ($indexType) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $indexType of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
103
                foreach ($items as $item) {
104
                    $key = $this->injectedTaxonKey.'_'.$indexType.'_index_permalinks';
105
                    $taxonIndexPermalinks = $item->data()->get($key) ?: [];
106
107
                    $taxonIndexPermalinks[$taxon] = $permalink;
108
109
                    $item->data()->set($key, $taxonIndexPermalinks);
110
                }
111
            }
112
113
            $generatedSources[] = $generatedSource;
114
        }
115
116
        return $generatedSources;
117
    }
118
}
119