Completed
Push — master ( 1f03c4...ae54df )
by André
47:22 queued 24:43
created

ContentContext::getCurrentDraft()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @license For full copyright and license information view LICENSE file distributed with this source code.
4
 */
5
namespace eZ\Bundle\EzPublishCoreBundle\Features\Context;
6
7
use Behat\Behat\Context\Context;
8
use Behat\Behat\Context\SnippetAcceptingContext;
9
use eZ\Publish\API\Repository\Repository;
10
use eZ\Publish\API\Repository\Values\Content\Content;
11
use RuntimeException;
12
13
class ContentContext implements Context, SnippetAcceptingContext
14
{
15
    /** @var \eZ\Publish\API\Repository\Values\Content\Content */
16
    private $currentContent;
17
18
    /** @var \eZ\Publish\API\Repository\Values\Content\Content */
19
    private $currentDraft;
20
21
    /** @var \eZ\Publish\API\Repository\Repository */
22
    private $repository;
23
24
    public function __construct(Repository $repository)
25
    {
26
        $this->repository = $repository;
27
    }
28
29
    /**
30
     * @Given /^I create an folder draft$/
31
     */
32
    public function iCreateAnFolderDraft()
33
    {
34
        $this->currentDraft = $this->createDraft(
35
            'folder',
36
            [
37
                'name' => 'Preview draft ' . date('c'),
38
                'short_description' => '<?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>',
39
            ]
40
        );
41
    }
42
43
    /**
44
     * @Given /^I create a draft of an existing content item$/
45
     */
46
    public function iCreateADraftOfAnExistingContentItem()
47
    {
48
        $this->currentContent = $this->createContentItem(
49
            'folder',
50
            ['name' => 'BDD preview test']
51
        );
52
53
        $this->currentDraft = $this->createDraftForContent($this->currentContent);
54
    }
55
56
    /**
57
     * Uses a content type identifier + a hash of fields values
58
     * to create and publish a content item below the root location.
59
     *
60
     * @param string $contentTypeIdentifier
61
     * @param array $fields Hash of field def identifier => field value
62
     *
63
     * @return Content the created content item.
64
     */
65
    public function createContentItem($contentTypeIdentifier, array $fields)
66
    {
67
        $draft = $this->createDraft($contentTypeIdentifier, $fields);
68
69
        $this->currentContent = $this->repository->sudo(
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface eZ\Publish\API\Repository\Repository as the method sudo() does only exist in the following implementations of said interface: eZ\Publish\Core\Repository\Repository, eZ\Publish\Core\SignalSlot\Repository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
70
            function () use ($draft) {
71
                return $this->repository->getContentService()->publishVersion($draft->versionInfo);
72
            }
73
        );
74
75
        $this->currentDraft = null;
76
77
        return $this->currentContent;
78
    }
79
80
    public function createDraftForContent(Content $content)
81
    {
82
        $this->currentDraft = $this->repository->sudo(
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface eZ\Publish\API\Repository\Repository as the method sudo() does only exist in the following implementations of said interface: eZ\Publish\Core\Repository\Repository, eZ\Publish\Core\SignalSlot\Repository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
83
            function () use ($content) {
84
                return $this->repository->getContentService()->createContentDraft($content->contentInfo);
85
            }
86
        );
87
88
        return $this->currentDraft;
89
    }
90
91
    public function getCurrentDraft()
92
    {
93
        if ($this->currentDraft === null) {
94
            throw new RuntimeException('No current draft has been set');
95
        }
96
97
        return $this->currentDraft;
98
    }
99
100
    public function updateDraft($fields)
101
    {
102
        $contentService = $this->repository->getContentService();
103
104
        $updateStruct = $contentService->newContentUpdateStruct();
105
        foreach ($fields as $fieldDefIdentifier => $fieldValueUpdate) {
106
            $updateStruct->setField($fieldDefIdentifier, $fieldValueUpdate);
107
        }
108
109
        $updatedDraft = $this->repository->sudo(function () use ($updateStruct) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface eZ\Publish\API\Repository\Repository as the method sudo() does only exist in the following implementations of said interface: eZ\Publish\Core\Repository\Repository, eZ\Publish\Core\SignalSlot\Repository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
110
            return $this->repository->getContentService()->updateContent(
111
                $this->currentDraft->versionInfo,
112
                $updateStruct
113
            );
114
        });
115
116
        return $this->currentDraft = $updatedDraft;
117
    }
118
119
    /**
120
     * Uses a content type identifier + a hash of fields values
121
     * to create and publish a draft below the root location.
122
     *
123
     * @param string $contentTypeIdentifier
124
     * @param array $fields Hash of field def identifier => field value
125
     *
126
     * @return Content the created draft.
127
     */
128
    public function createDraft($contentTypeIdentifier, array $fields)
129
    {
130
        $contentService = $this->repository->getContentService();
131
132
        $createStruct = $contentService->newContentCreateStruct(
133
            $this->repository->getContentTypeService()->loadContentTypeByIdentifier($contentTypeIdentifier),
134
            'eng-GB'
135
        );
136
137
        foreach ($fields as $fieldDefIdentifier => $fieldValue) {
138
            $createStruct->setField($fieldDefIdentifier, $fieldValue);
139
        }
140
141
        $locationCreateStruct = $this->repository->getLocationService()->newLocationCreateStruct(2);
142
143
        $this->currentDraft = $this->repository->sudo(
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface eZ\Publish\API\Repository\Repository as the method sudo() does only exist in the following implementations of said interface: eZ\Publish\Core\Repository\Repository, eZ\Publish\Core\SignalSlot\Repository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
144
            function () use ($createStruct, $locationCreateStruct) {
145
                return $this->repository->getContentService()->createContent(
146
                    $createStruct,
147
                    [$locationCreateStruct]
148
                );
149
            }
150
        );
151
152
        return $this->currentDraft;
153
    }
154
}
155