Completed
Push — ezp-30696 ( 9bb3ad...3bd812 )
by
unknown
49:02 queued 18:35
created

ContentUpdate::parse()   F

Complexity

Conditions 20
Paths 707

Size

Total Lines 68

Duplication

Lines 14
Ratio 20.59 %

Importance

Changes 0
Metric Value
cc 20
nc 707
nop 2
dl 14
loc 68
rs 0.4069
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * File containing the ContentUpdate parser class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\REST\Server\Input\Parser;
10
11
use eZ\Publish\Core\REST\Common\Input\BaseParser;
12
use eZ\Publish\Core\REST\Common\Input\ParsingDispatcher;
13
use eZ\Publish\Core\REST\Common\Exceptions;
14
use eZ\Publish\Core\REST\Common\Values\RestContentMetadataUpdateStruct;
15
use DateTime;
16
use Exception;
17
18
/**
19
 * Parser for ContentUpdate.
20
 */
21
class ContentUpdate extends BaseParser
22
{
23
    /**
24
     * Parse input structure.
25
     *
26
     * @todo use url handler instead of hardcoded URL matching
27
     *
28
     * @param array $data
29
     * @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher
30
     *
31
     * @return \eZ\Publish\Core\REST\Common\Values\RestContentMetadataUpdateStruct
32
     *
33
     * @throws \eZ\Publish\Core\REST\Common\Exceptions\Parser if $data is invalid
34
     */
35
    public function parse(array $data, ParsingDispatcher $parsingDispatcher)
36
    {
37
        $parsedData = [];
38
39
        if (array_key_exists('Section', $data) && is_array($data['Section']) && isset($data['Section']['_href'])) {
40
            try {
41
                $parsedData['sectionId'] = $this->requestParser->parseHref($data['Section']['_href'], 'sectionId');
42
            } catch (Exceptions\InvalidArgumentException $e) {
43
                throw new Exceptions\Parser('Invalid format for <Section> reference in <ContentUpdate>.');
44
            }
45
        }
46
47
        if (array_key_exists('Owner', $data) && is_array($data['Owner']) && isset($data['Owner']['_href'])) {
48
            try {
49
                $parsedData['ownerId'] = $this->requestParser->parseHref($data['Owner']['_href'], 'userId');
50
            } catch (Exceptions\InvalidArgumentException $e) {
51
                throw new Exceptions\Parser('Invalid format for <Owner> reference in <ContentUpdate>.');
52
            }
53
        }
54
55
        if (array_key_exists('mainLanguageCode', $data)) {
56
            $parsedData['mainLanguageCode'] = $data['mainLanguageCode'];
57
        }
58
59
        if (array_key_exists('MainLocation', $data)) {
60
            try {
61
                $mainLocationIdParts = explode('/', $this->requestParser->parseHref($data['MainLocation']['_href'], 'locationPath'));
62
                $parsedData['mainLocationId'] = array_pop($mainLocationIdParts);
63
            } catch (Exceptions\InvalidArgumentException $e) {
64
                throw new Exceptions\Parser('Invalid format for <MainLocation> reference in <ContentUpdate>.');
65
            }
66
        }
67
68
        if (array_key_exists('alwaysAvailable', $data)) {
69
            if ($data['alwaysAvailable'] === 'true') {
70
                $parsedData['alwaysAvailable'] = true;
71
            } elseif ($data['alwaysAvailable'] === 'false') {
72
                $parsedData['alwaysAvailable'] = false;
73
            } else {
74
                throw new Exceptions\Parser('Invalid format for <alwaysAvailable> in <ContentUpdate>.');
75
            }
76
        }
77
78
        // remoteId
79
        if (array_key_exists('remoteId', $data)) {
80
            $parsedData['remoteId'] = $data['remoteId'];
81
        }
82
83
        // modificationDate
84 View Code Duplication
        if (array_key_exists('modificationDate', $data)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
85
            try {
86
                $parsedData['modificationDate'] = new DateTime($data['modificationDate']);
87
            } catch (Exception $e) {
88
                throw new Exceptions\Parser('Invalid format for <modificationDate> in <ContentUpdate>', 0, $e);
89
            }
90
        }
91
92
        // publishDate
93 View Code Duplication
        if (array_key_exists('publishDate', $data)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
94
            try {
95
                $parsedData['publishedDate'] = new DateTime($data['publishDate']);
96
            } catch (Exception $e) {
97
                throw new Exceptions\Parser('Invalid format for <publishDate> in <ContentUpdate>', 0, $e);
98
            }
99
        }
100
101
        return new RestContentMetadataUpdateStruct($parsedData);
102
    }
103
}
104