Completed
Push — master ( 6343d0...45320b )
by Bernhard
06:10
created

ChainVersioner::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the webmozart/json package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\Manager\Json;
13
14
use stdClass;
15
use Webmozart\Json\Versioning\CannotParseVersionException;
16
use Webmozart\Json\Versioning\CannotUpdateVersionException;
17
use Webmozart\Json\Versioning\JsonVersioner;
18
19
/**
20
 * @since  1.0
21
 *
22
 * @author Bernhard Schussek <[email protected]>
23
 */
24
class ChainVersioner implements JsonVersioner
25
{
26
    /**
27
     * @var JsonVersioner[]
28
     */
29
    private $versioners;
30
31
    /**
32
     * @param JsonVersioner[] $versioners
33
     */
34 26
    public function __construct(array $versioners)
35
    {
36 26
        $this->versioners = $versioners;
37 26
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 26 View Code Duplication
    public function parseVersion(stdClass $jsonData)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44
        /** @var CannotParseVersionException $firstException */
45 26
        $firstException = null;
46
47 26
        foreach ($this->versioners as $versioner) {
48
            try {
49 26
                return $versioner->parseVersion($jsonData);
50 26
            } catch (CannotParseVersionException $e) {
51 26
                if (null === $firstException) {
52 26
                    $firstException = $e;
53
                }
54
            }
55
        }
56
57
        throw $firstException;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 26 View Code Duplication
    public function updateVersion(stdClass $jsonData, $version)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        /** @var CannotUpdateVersionException $firstException */
66 26
        $firstException = null;
67
68 26
        foreach ($this->versioners as $versioner) {
69
            try {
70 26
                $versioner->updateVersion($jsonData, $version);
71
72 26
                return;
73
            } catch (CannotUpdateVersionException $e) {
74
                if (null === $firstException) {
75
                    $firstException = $e;
76
                }
77
            }
78
        }
79
80
        throw $firstException;
81
    }
82
}
83