Completed
Push — EZP-30997 ( 88c2f1...8c90bc )
by
unknown
45:26 queued 28:35
created

VersionBuilder::publishTranslations()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
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
    /** @var array */
24
    private $targetVersionProperties = [];
25
26
    public function build(): Target\Version
27
    {
28
        return new Target\Version($this->targetVersionProperties);
29
    }
30
31
    /**
32
     * Set intent to translate, to an unspecified (yet) language, any from the given list.
33
     *
34
     * @param array $languageCodes
35
     *
36
     * @return self
37
     *
38
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
39
     */
40
    public function translateToAnyLanguageOf(array $languageCodes): self
41
    {
42
        foreach ($languageCodes as $languageCode) {
43
            if (!is_string($languageCode) || empty($languageCode)) {
44
                throw new InvalidArgumentException('$languageCodes', 'All language codes should be non-empty strings');
45
            }
46
        }
47
48
        $this->targetVersionProperties['allLanguageCodesList'] = $languageCodes;
49
50
        return $this;
51
    }
52
53
    /**
54
     * Set intent to create Content from unspecified (yet) content type, any from the given list.
55
     *
56
     * @param int[] $contentTypeIds
57
     *
58
     * @return self
59
     *
60
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
61
     */
62
    public function createFromAnyContentTypeOf(array $contentTypeIds): self
63
    {
64
        foreach ($contentTypeIds as $contentTypeId) {
65
            if (!\is_int($contentTypeId)) {
66
                throw new InvalidArgumentException('$contentTypeIds', 'All contentType ids should be integers');
67
            }
68
        }
69
70
        $this->targetVersionProperties['allContentTypeIdsList'] = $contentTypeIds;
71
72
        return $this;
73
    }
74
75
    /**
76
     * Set intent to change Version status.
77
     *
78
     * Supported: <code>VersionInfo::STATUS_DRAFT, VersionInfo::STATUS_PUBLISHED, VersionInfo::STATUS_ARCHIVED</code>
79
     *
80
     * @see \eZ\Publish\API\Repository\Values\Content\VersionInfo
81
     *
82
     * @param int $status
83
     *
84
     * @return self
85
     *
86
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
87
     */
88
    public function changeStatusTo(int $status): self
89
    {
90
        if (!in_array(
91
            $status,
92
            [VersionInfo::STATUS_DRAFT, VersionInfo::STATUS_PUBLISHED, VersionInfo::STATUS_ARCHIVED]
93
        )) {
94
            throw new InvalidArgumentException(
95
                '$status',
96
                'Status should be one of the following: STATUS_DRAFT, STATUS_PUBLISHED, STATUS_ARCHIVED'
97
            );
98
        }
99
100
        $this->targetVersionProperties['newStatus'] = $status;
101
102
        return $this;
103
    }
104
105
    /**
106
     * Set intent to update Content Version Fields.
107
     *
108
     * @param string|null $initialLanguageCode
109
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
110
     *
111
     * @return self
112
     */
113
    public function updateFieldsTo(?string $initialLanguageCode, array $fields): self
114
    {
115
        $languageCodes = array_map(
116
            function (Field $field) {
117
                return $field->languageCode;
118
            },
119
            $fields
120
        );
121
122
        $this->targetVersionProperties['forUpdateInitialLanguageCode'] = $initialLanguageCode;
123
        $this->targetVersionProperties['forUpdateLanguageCodesList'] = array_values(
124
            array_unique($languageCodes)
125
        );
126
127
        return $this;
128
    }
129
130
    /**
131
     * Set intent to publish, to specified translations, all from the given list.
132
     *
133
     * @param array $languageCodes
134
     *
135
     * @return self
136
     *
137
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
138
     */
139
    public function publishTranslations(array $languageCodes): self
140
    {
141
        foreach ($languageCodes as $languageCode) {
142
            if (!is_string($languageCode) || empty($languageCode)) {
143
                throw new InvalidArgumentException('$languageCodes', 'All language codes should be non-empty strings');
144
            }
145
        }
146
147
        $this->targetVersionProperties['forPublishLanguageCodesList'] = $languageCodes;
148
149
        return $this;
150
    }
151
}
152