Completed
Push — EZP-30470 ( 9c9496 )
by
unknown
38:05 queued 16:21
created

VersionBuilder::createFromAnyContentTypeOf()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\SPI\Limitation\Target\Builder;
10
11
use eZ\Publish\API\Repository\Values\Content\Field;
12
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
13
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
14
use eZ\Publish\SPI\Limitation\Target;
15
16
/**
17
 * Builder of \eZ\Publish\SPI\Limitation\Target\Version instance.
18
 *
19
 * @see \eZ\Publish\SPI\Limitation\Target\Version
20
 */
21
final class VersionBuilder
22
{
23
    /**
24
     * @var array
25
     */
26
    private $targetVersionProperties = [];
27
28
    public function build(): Target\Version
29
    {
30
        return new Target\Version($this->targetVersionProperties);
31
    }
32
33
    /**
34
     * Set intent to translate, to an unspecified (yet) language, any from the given list.
35
     *
36
     * @param array $languageCodes
37
     *
38
     * @return self
39
     *
40
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
41
     */
42
    public function translateToAnyLanguageOf(array $languageCodes): self
43
    {
44
        foreach ($languageCodes as $languageCode) {
45
            if (!is_string($languageCode) || empty($languageCode)) {
46
                throw new InvalidArgumentException('$languageCodes', 'All language codes should be non-empty strings');
47
            }
48
        }
49
50
        $this->targetVersionProperties['allLanguageCodesList'] = $languageCodes;
51
52
        return $this;
53
    }
54
55
    /**
56
     * Set intent to create Content from unspecified (yet) content type, any from the given list.
57
     *
58
     * @param array $contentTypeIds
59
     *
60
     * @return self
61
     *
62
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
63
     */
64
    public function createFromAnyContentTypeOf(array $contentTypeIds): self
65
    {
66
        foreach ($contentTypeIds as $contentTypeId) {
67
            if (!\is_string($contentTypeId)) {
68
                throw new InvalidArgumentException('$contentTypeIds', 'All contentType ids should be strings');
69
            }
70
        }
71
72
        $this->targetVersionProperties['allContentTypeIdsList'] = $contentTypeIds;
73
74
        return $this;
75
    }
76
77
    /**
78
     * Set intent to change Version status.
79
     *
80
     * Supported: <code>VersionInfo::STATUS_DRAFT, VersionInfo::STATUS_PUBLISHED, VersionInfo::STATUS_ARCHIVED</code>
81
     *
82
     * @see \eZ\Publish\API\Repository\Values\Content\VersionInfo
83
     *
84
     * @param int $status
85
     *
86
     * @return self
87
     *
88
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
89
     */
90
    public function changeStatusTo(int $status): self
91
    {
92
        if (!in_array(
93
            $status,
94
            [VersionInfo::STATUS_DRAFT, VersionInfo::STATUS_PUBLISHED, VersionInfo::STATUS_ARCHIVED]
95
        )) {
96
            throw new InvalidArgumentException(
97
                '$status',
98
                'Status should be one of the following: STATUS_DRAFT, STATUS_PUBLISHED, STATUS_ARCHIVED'
99
            );
100
        }
101
102
        $this->targetVersionProperties['newStatus'] = $status;
103
104
        return $this;
105
    }
106
107
    /**
108
     * Set intent to update Content Version Fields.
109
     *
110
     * @param string|null $initialLanguageCode
111
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
112
     *
113
     * @return self
114
     */
115
    public function updateFieldsTo(?string $initialLanguageCode, array $fields): self
116
    {
117
        $languageCodes = array_map(
118
            function (Field $field) {
119
                return $field->languageCode;
120
            },
121
            $fields
122
        );
123
124
        $this->targetVersionProperties['forUpdateInitialLanguageCode'] = $initialLanguageCode;
125
        $this->targetVersionProperties['forUpdateLanguageCodesList'] = array_values(
126
            array_unique($languageCodes)
127
        );
128
129
        return $this;
130
    }
131
}
132