UrlAliasManager::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Executor;
4
5
use Kaliop\eZMigrationBundle\API\Exception\InvalidStepDefinitionException;
6
use Kaliop\eZMigrationBundle\Core\Matcher\UrlAliasMatcher;
7
8
/**
9
 * @todo allow refreshing non-custom location aliases
10
 */
11
class UrlAliasManager extends RepositoryExecutor
12
{
13
    protected $supportedStepTypes = array('url_alias');
14
    protected $supportedActions = array('create', 'load', 'cleanup', 'regenerate');
15
16
    protected $urlAliasMatcher;
17
    protected $locationManager;
18
19 149
    public function __construct(UrlAliasMatcher $urlAliasMatcher, LocationManager $locationManager)
20
    {
21 149
        $this->urlAliasMatcher = $urlAliasMatcher;
22 149
        $this->locationManager = $locationManager;
23 149
    }
24
25
    protected function create($step)
26
    {
27
        $urlAliasService = $this->repository->getUrlAliasService();
28
29
        if (!isset($step->dsl['destination']) && !isset($step->dsl['destination_location'])) {
30
            throw new InvalidStepDefinitionException("Either the 'destination' or 'destination_location' key is required to create a new urlalias.");
31
        }
32
        if (isset($step->dsl['destination']) && isset($step->dsl['destination_location'])) {
33
            throw new InvalidStepDefinitionException("The 'destination' and 'destination_location' keys can not be used at the same time to create a new urlalias.");
34
        }
35
        // be kind to users
36
        if (!isset($step->dsl['source']) && !isset($step->dsl['path'])) {
37
            throw new InvalidStepDefinitionException("The 'source' key is required to create a new urlalias.");
38
        }
39
        if (isset($step->dsl['source'])) {
40
            $path = $this->resolveReference($step->dsl['source']);
41
        } else {
42
            $path = $this->resolveReference($step->dsl['path']);
43
        }
44
45
        $languageCode = isset($step->dsl['language_code']) ? $this->resolveReference($step->dsl['language_code']) : null;
46
47
        $forward = isset($step->dsl['forward']) ? $this->resolveReference($step->dsl['forward']) : false;
48
49
        $alwaysAvailable = isset($step->dsl['always_available']) ? $this->resolveReference($step->dsl['always_available']) : false;
50
51
        if (isset($step->dsl['destination'])) {
52
            $url = $urlAliasService->createGlobalUrlAlias($this->resolveReference($step->dsl['destination']), $path, $languageCode, $forward, $alwaysAvailable);
53
        } else {
54
            /// @todo Should we always resolve refs in $step->dsl['destination_location']? What if it is a remote_id in the form of 'reference:hello'
55
            $destination = $this->resolveReference($step->dsl['destination_location']);
56
57
            if (!is_int($destination) && !ctype_digit($destination)) {
58
                $location = $this->repository->getLocationService()->loadLocationByRemoteId($destination);
59
            } else {
60
                $location = $this->repository->getLocationService()->loadLocation($destination);
61
            }
62
63
            $url = $urlAliasService->createUrlAlias($location, $path, $languageCode, $forward, $alwaysAvailable);
64
        }
65
66
        $this->setReferences($url, $step);
67
68
        return $url;
69
    }
70
71
    protected function load($step)
72
    {
73
        $urlCollection = $this->matchUrlAlias('load', $step);
74
75
        $this->validateResultsCount($urlCollection, $step);
76
77
        $this->setReferences($urlCollection, $step);
78
79
        return $urlCollection;
80
    }
81
82
    protected function delete($step)
83
    {
84
        $urlCollection = $this->matchUrlAlias('delete', $step);
85
86
        $this->validateResultsCount($urlCollection, $step);
87
88
        $this->setReferences($urlCollection, $step);
89
90
        $urlAliasService = $this->repository->getUrlAliasService();
91
92
        foreach ($urlCollection as $url) {
93
            $urlAliasService->removeAliases([$url]);
94
        }
95
96
        return $urlCollection;
97
    }
98
99
    protected function regenerate($step)
100
    {
101
        $urlAliasService = $this->repository->getUrlAliasService();
102
103
        foreach ($this->locationManager->matchLocations('refresh the system url aliases for', $step) as $location)
104
        {
105
            $urlAliasService->refreshSystemUrlAliasesForLocation($location);
0 ignored issues
show
Bug introduced by
The method refreshSystemUrlAliasesForLocation() does not exist on eZ\Publish\API\Repository\URLAliasService. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
            $urlAliasService->/** @scrutinizer ignore-call */ 
106
                              refreshSystemUrlAliasesForLocation($location);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
106
        }
107
108
        return true;
109
    }
110
111
    protected function cleanup($step)
0 ignored issues
show
Unused Code introduced by
The parameter $step is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

111
    protected function cleanup(/** @scrutinizer ignore-unused */ $step)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
    {
113
        $urlAliasService = $this->repository->getUrlAliasService();
114
115
        return $urlAliasService->deleteCorruptedUrlAliases();
0 ignored issues
show
Bug introduced by
The method deleteCorruptedUrlAliases() does not exist on eZ\Publish\API\Repository\URLAliasService. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

115
        return $urlAliasService->/** @scrutinizer ignore-call */ deleteCorruptedUrlAliases();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
116
    }
117
118
    protected function matchUrlAlias($action, $step)
119
    {
120
        if (!isset($step->dsl['match'])) {
121
            throw new InvalidStepDefinitionException("A match condition is required to $action an urlalias");
122
        }
123
124
        // convert the references passed in the match
125
        $match = $this->resolveReferencesRecursively($step->dsl['match']);
126
127
        $tolerateMisses = isset($step->dsl['match_tolerate_misses']) ? $this->resolveReference($step->dsl['match_tolerate_misses']) : false;
128
129
        return $this->urlAliasMatcher->match($match, $tolerateMisses);
130
    }
131
132
    protected function getReferencesValues($urlAlias, array $references, $step)
133
    {
134
        $refs = array();
135
136
        foreach ($references as $key => $reference) {
137
138
            $reference = $this->parseReferenceDefinition($key, $reference);
139
140
            switch ($reference['attribute']) {
141
                case 'url_id':
142
                case 'id':
143
                    $value = $urlAlias->id;
144
                    break;
145
                case 'type':
146
                    $value = $urlAlias->type;
147
                    break;
148
                case 'destination':
149
                    $value = $urlAlias->destination;
150
                    break;
151
                case 'path':
152
                case 'source':
153
                    $value = $urlAlias->path;
154
                    break;
155
                case 'language_codes':
156
                    /// @todo should we return a string?
157
                    $value = $urlAlias->languageCodes;
158
                    break;
159
                case 'always_available':
160
                    $value = $urlAlias->alwaysAvailable;
161
                    break;
162
                case 'is_history':
163
                    $value = $urlAlias->isHistory;
164
                    break;
165
                case 'is_custom':
166
                    $value = $urlAlias->isCustom;
167
                    break;
168
                case 'forward':
169
                    $value = $urlAlias->forward;
170
                    break;
171
                default:
172
                    throw new InvalidStepDefinitionException('UrlAlias Manager does not support setting references for attribute ' . $reference['attribute']);
173
            }
174
175
            $refs[$reference['identifier']] = $value;
176
        }
177
178
        return $refs;
179
    }
180
}
181