Completed
Push — ezp26175-exception_on_non_defa... ( 77d2f3...ca5fc8 )
by
unknown
39:33
created

BasicContentContext   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 190
Duplicated Lines 9.47 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 18
loc 190
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 3

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A createContent() 0 9 1
A publishDraft() 0 4 1
A createContentDraft() 0 13 3
A createContentWithPath() 0 19 4
A getContentPath() 0 4 1
A mapContentPath() 0 5 1
A createBasicFolder() 0 6 1
A createBasicArticle() 9 9 1
A createArticleDraft() 9 9 1
A getTitleFromPath() 0 6 1
A getDummyXmlText() 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
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
4
 * @license For full copyright and license information view LICENSE file distributed with this source code.
5
 * @version //autogentag//
6
 */
7
namespace eZ\Bundle\EzPublishCoreBundle\Features\Context;
8
9
use Behat\Behat\Context\Context;
10
use EzSystems\PlatformBehatBundle\Context\RepositoryContext;
11
use eZ\Publish\API\Repository\ContentTypeService;
12
use eZ\Publish\API\Repository\ContentService;
13
use eZ\Publish\API\Repository\Repository;
14
15
/**
16
 * Sentences for simple Contents creation.
17
 */
18
class BasicContentContext implements Context
19
{
20
    use RepositoryContext;
21
22
    /**
23
     * Default language.
24
     */
25
    const DEFAULT_LANGUAGE = 'eng-GB';
26
27
    /**
28
     * Content path mapping.
29
     */
30
    private $contentPaths = array();
31
32
    /**
33
     * @var eZ\Publish\API\Repository\ContentTypeService
34
     */
35
    private $contentTypeService;
36
37
    /**
38
     * @var eZ\Publish\API\Repository\ContentService
39
     */
40
    private $contentService;
41
42
    /**
43
     * @injectService $repository @ezpublish.api.repository
44
     * @injectService $contentTypeService @ezpublish.api.service.content_type
45
     * @injectService $contentService @ezpublish.api.service.content
46
     */
47
    public function __construct(
48
        Repository $repository,
49
        ContentTypeService $contentTypeService,
50
        ContentService $contentService
51
    ) {
52
        $this->setRepository($repository);
53
        $this->contentTypeService = $contentTypeService;
0 ignored issues
show
Documentation Bug introduced by
It seems like $contentTypeService of type object<eZ\Publish\API\Re...ory\ContentTypeService> is incompatible with the declared type object<eZ\Bundle\EzPubli...ory\ContentTypeService> of property $contentTypeService.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
54
        $this->contentService = $contentService;
0 ignored issues
show
Documentation Bug introduced by
It seems like $contentService of type object<eZ\Publish\API\Repository\ContentService> is incompatible with the declared type object<eZ\Bundle\EzPubli...ository\ContentService> of property $contentService.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
55
    }
56
57
    /**
58
     * Creates and publishes a Content.
59
     *
60
     * @param string $contentType
61
     * @param array $fields
62
     * @param mixed $parentLocationId
63
     *
64
     * @return mixed The content's main location id
65
     */
66
    public function createContent($contentType, $fields, $parentLocationId)
67
    {
68
        $repository = $this->getRepository();
0 ignored issues
show
Unused Code introduced by
$repository is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
69
        $languageCode = self::DEFAULT_LANGUAGE;
70
        $content = $this->createContentDraft($parentLocationId, $contentType, $fields, $languageCode);
71
        $content = $this->contentService->publishVersion($content->versionInfo);
72
73
        return $content->contentInfo->mainLocationId;
74
    }
75
76
    /**
77
     * Publishes a content draft.
78
     *
79
     * @param eZ\Publish\API\Repository\Values\Content\Content $content
80
     */
81
    public function publishDraft(Content $content)
82
    {
83
        $this->contentService->publishVersion($content->versionInfo->id);
84
    }
85
86
    /**
87
     * Creates a content draft.
88
     *
89
     * @param eZ\Publish\API\Repository\Values\Content\Location $parentLocationId
90
     * @param string $contentTypeIdentifier
91
     * @param string $languageCode
92
     * @param array $fields Fields, as primitives understood by setField
93
     *
94
     * @return eZ\Publish\API\Repository\Values\Content\Content an unpublished Content draft
95
     */
96
    public function createContentDraft($parentLocationId, $contentTypeIdentifier, $fields, $languageCode = null)
97
    {
98
        $languageCode = $languageCode ?: self::DEFAULT_LANGUAGE;
99
        $repository = $this->getRepository();
100
        $locationCreateStruct = $repository->getLocationService()->newLocationCreateStruct($parentLocationId);
101
        $contentTypeIdentifier = $this->contentTypeService->loadContentTypeByIdentifier($contentTypeIdentifier);
102
        $contentCreateStruct = $this->contentService->newContentCreateStruct($contentTypeIdentifier, $languageCode);
103
        foreach (array_keys($fields) as $key) {
104
            $contentCreateStruct->setField($key, $fields[$key]);
105
        }
106
107
        return $this->contentService->createContent($contentCreateStruct, array($locationCreateStruct));
108
    }
109
110
    /**
111
     * Creates and publishes a content at a given path.
112
     * Non-existing path items are created as folders named after the path element.
113
     *
114
     * @param string $path The content path
115
     * @param array $fields
116
     * @param mixed $contentType The content type identifier
117
     *
118
     * @return mixed location id of the created content
119
     */
120
    public function createContentWithPath($path, $fields, $contentType)
121
    {
122
        $contentsName = explode('/', $path);
123
        $currentPath = '';
124
        $location = '2';
125
        foreach ($contentsName as $name) {
126
            if ($name != end($contentsName)) {
127
                $location = $this->createContent('folder', ['name' => $name], $location);
128
            }
129
            if ($currentPath != '') {
130
                $currentPath .= '/';
131
            }
132
            $currentPath .= $name;
133
            $this->mapContentPath($currentPath);
134
        }
135
        $location = $this->createContent($contentType, $fields, $location);
136
137
        return $location;
138
    }
139
140
    /**
141
     * Getter for contentPaths.
142
     */
143
    public function getContentPath($name)
144
    {
145
        return $this->contentPaths[$name];
146
    }
147
148
    /**
149
     * Maps the path of the content to it's name for later use.
150
     */
151
    private function mapContentPath($path)
152
    {
153
        $contentNames = explode('/', $path);
154
        $this->contentPaths[end($contentNames)] = $path;
155
    }
156
157
    /**
158
     * @Given a/an :path folder exists
159
     */
160
    public function createBasicFolder($path)
161
    {
162
        $fields = array('name' => $this->getTitleFromPath($path));
163
164
        return $this->createContentwithPath($path, $fields, 'folder');
165
    }
166
167
    /**
168
     * @Given a/an :path article exists
169
     */
170 View Code Duplication
    public function createBasicArticle($path)
171
    {
172
        $fields = array(
173
            'title' => $this->getTitleFromPath($path),
174
            'intro' => $this->getDummyXmlText(),
175
        );
176
177
        return $this->createContentwithPath($path, $fields, 'article');
178
    }
179
180
    /**
181
     * @Given a/an :path article draft exists
182
     */
183 View Code Duplication
    public function createArticleDraft($path)
184
    {
185
        $fields = array(
186
            'title' => $this->getTitleFromPath($path),
187
            'intro' => $this->getDummyXmlText(),
188
        );
189
190
        return $this->createContentDraft(2, 'article', $fields);
191
    }
192
193
    private function getTitleFromPath($path)
194
    {
195
        $parts = explode('/', rtrim($path, '/'));
196
197
        return end($parts);
198
    }
199
200
    /**
201
     * @return string
202
     */
203
    private function getDummyXmlText()
204
    {
205
        return '<?xml version="1.0" encoding="UTF-8"?><section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ez.no/xmlns/ezpublish/docbook/xhtml" xmlns:ezcustom="http://ez.no/xmlns/ezpublish/docbook/custom" version="5.0-variant ezpublish-1.0"><para>This is a paragraph.</para></section>';
206
    }
207
}
208