StatusToStateAttribute   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 65
rs 10
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B getState() 0 56 7
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * @author hashashiyyin [email protected] / [email protected]
5
 * Date: 02/08/24
6
 * Time: 13:11
7
 *
8
 */
9
10
namespace Matecat\XliffParser\XliffReplacer;
11
12
use Matecat\XliffParser\Constants\TranslationStatus;
13
14
class StatusToStateAttribute {
15
16
    /**
17
     * @param int         $xliffVersion
18
     * @param string|null $status
19
     * @param string|null $lastMrkState
20
     *
21
     * @return array
22
     */
23
    public static function getState(
24
            int     $xliffVersion,
25
            ?string $status = null,
26
            ?string $lastMrkState = null
27
    ): array {
28
29
        $status = empty( $status ) ? TranslationStatus::STATUS_APPROVED2 : $status;
30
31
        $stateLevelsMap = [
32
                TranslationStatus::STATUS_APPROVED2  => 100,
33
                TranslationStatus::STATUS_APPROVED   => 90,
34
                TranslationStatus::STATUS_TRANSLATED => 80,
35
                TranslationStatus::STATUS_REJECTED   => 70,
36
                TranslationStatus::STATUS_DRAFT      => 60,
37
                TranslationStatus::STATUS_NEW        => 50
38
        ];
39
40
        $orderedValues = array_flip( $stateLevelsMap );
41
42
        // Define state mappings for different statuses
43
        $stateMap = [
44
                TranslationStatus::STATUS_APPROVED2  => [ "state=\"final\"", TranslationStatus::STATUS_APPROVED2 ],
45
                TranslationStatus::STATUS_APPROVED   => [
46
                        ( $xliffVersion === 2 ) ? "state=\"reviewed\"" : "state=\"signed-off\"",
47
                        TranslationStatus::STATUS_APPROVED
48
                ],
49
                TranslationStatus::STATUS_TRANSLATED => [ "state=\"translated\"", TranslationStatus::STATUS_TRANSLATED ],
50
                TranslationStatus::STATUS_REJECTED   => [
51
                        ( $xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"needs-review-translation\"",
52
                        TranslationStatus::STATUS_REJECTED
53
                ],
54
                TranslationStatus::STATUS_NEW        => [
55
                        ( $xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"new\"",
56
                        TranslationStatus::STATUS_NEW
57
                ],
58
                TranslationStatus::STATUS_DRAFT      => [
59
                        ( $xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"new\"",
60
                        TranslationStatus::STATUS_DRAFT
61
                ],
62
        ];
63
64
        // If status is null we set the default status value as Approved2 because in this way
65
        // it will not affect the result of the min() function.
66
        // This is the case when a segment is not shown in the cattool,
67
        // and the row in segment_translations does not exists.
68
        // ---> $seg[ 'status' ] is NULL
69
        // If lastMrkState is empty
70
        $minStatus = min(
71
                $stateLevelsMap[ $status ],
72
                ( $stateLevelsMap[ $lastMrkState ] ?? $stateLevelsMap[ TranslationStatus::STATUS_NEW ] )
73
        );
74
75
        // If the last mark state is set, get the minimum value, otherwise get the current state
76
        [ $state_prop, $lastMrkState ] = empty( $lastMrkState ) ? $stateMap[ $status ] : $stateMap[ $orderedValues[ $minStatus ] ];
77
78
        return [ $state_prop, $lastMrkState ];
79
80
    }
81
82
}