Completed
Push — master ( 83fb2b...0f847f )
by
unknown
42:12 queued 20:32
created

VersionBuilder   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 89
c 0
b 0
f 0
rs 10
wmc 8
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 4 1
A translateToAnyLanguageOf() 0 12 4
A changeStatusTo() 0 16 2
A updateFieldsTo() 0 16 1
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 change Version status.
57
     *
58
     * Supported: <code>VersionInfo::STATUS_DRAFT, VersionInfo::STATUS_PUBLISHED, VersionInfo::STATUS_ARCHIVED</code>
59
     *
60
     * @see \eZ\Publish\API\Repository\Values\Content\VersionInfo
61
     *
62
     * @param int $status
63
     *
64
     * @return self
65
     *
66
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
67
     */
68
    public function changeStatusTo(int $status): self
69
    {
70
        if (!in_array(
71
            $status,
72
            [VersionInfo::STATUS_DRAFT, VersionInfo::STATUS_PUBLISHED, VersionInfo::STATUS_ARCHIVED]
73
        )) {
74
            throw new InvalidArgumentException(
75
                '$status',
76
                'Status should be one of the following: STATUS_DRAFT, STATUS_PUBLISHED, STATUS_ARCHIVED'
77
            );
78
        }
79
80
        $this->targetVersionProperties['newStatus'] = $status;
81
82
        return $this;
83
    }
84
85
    /**
86
     * Set intent to update Content Version Fields.
87
     *
88
     * @param string|null $initialLanguageCode
89
     * @param \eZ\Publish\API\Repository\Values\Content\Field[] $fields
90
     *
91
     * @return self
92
     */
93
    public function updateFieldsTo(?string $initialLanguageCode, array $fields): self
94
    {
95
        $languageCodes = array_map(
96
            function (Field $field) {
97
                return $field->languageCode;
98
            },
99
            $fields
100
        );
101
102
        $this->targetVersionProperties['forUpdateInitialLanguageCode'] = $initialLanguageCode;
103
        $this->targetVersionProperties['forUpdateLanguageCodesList'] = array_values(
104
            array_unique($languageCodes)
105
        );
106
107
        return $this;
108
    }
109
}
110