Completed
Push — master ( f920cb...5251c6 )
by Maik
01:19
created

QueryResultParser::parseRelatedTopics()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 28
Code Lines 17

Duplication

Lines 17
Ratio 60.71 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 0
Metric Value
dl 17
loc 28
ccs 17
cts 17
cp 1
rs 8.439
c 0
b 0
f 0
cc 6
eloc 17
nc 6
nop 2
crap 6
1
<?php
2
namespace Nkey\DDG\API;
3
4
use Nkey\DDG\API\Model\Icon;
5
use Nkey\DDG\API\Model\QueryResult;
6
use Nkey\DDG\API\Model\RelatedTopic;
7
use Webmozart\Json\JsonDecoder;
8
use ReflectionClass;
9
use ReflectionMethod;
10
11
/**
12
 * This class provides means for parsing json from duckduckgo api into a local model
13
 *
14
 * @author Maik Greubel <[email protected]>
15
 * @license Apache 2.0
16
 */
17
class QueryResultParser
18
{
19
20
    /**
21
     * Parse a payload json string
22
     *
23
     * @param string $payload
24
     *            The payload to parse
25
     * @return QueryResult
26
     */
27 2
    public function parseQueryResult(string $payload): QueryResult
28
    {
29 2
        $decoder = new JsonDecoder();
30
        
31 2
        $json = $decoder->decode($payload);
32
        
33 2
        $result = new QueryResult();
34
        
35 2
        $ref = new ReflectionClass($result);
36 2
        $methods = $ref->getMethods(ReflectionMethod::IS_PUBLIC);
37 2 View Code Duplication
        foreach ($methods as $method) {
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...
38 2
            assert($method instanceof ReflectionMethod);
39
            
40 2
            if (substr($method->getName(), 0, 3) === 'set') {
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
41 2
                $propertyName = substr($method->getName(), 3);
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
42 2
                if ($propertyName === 'Meta') {
43 2
                    $result = $this->parseMeta($result, $json->meta);
44 2
                    continue;
45
                }
46 2
                if (property_exists($json, $propertyName)) {
47 2
                    $property = $json->$propertyName;
48 2
                    $method->invoke($result, $property);
49
                }
50
            }
51
        }
52
        
53 2
        $result = $this->parseRelatedTopics($result, $json->RelatedTopics);
54 2
        return $result;
55
    }
56
57 2
    private function parseRelatedTopics(QueryResult $result, $relatedTopics)
58
    {
59 2
        foreach ($relatedTopics as $relatedTopic) {
60 2
            $topic = new RelatedTopic();
61 2
            $ref = new ReflectionClass($topic);
62 2
            $methods = $ref->getMethods(ReflectionMethod::IS_PUBLIC);
63 2 View Code Duplication
            foreach ($methods as $method) {
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...
64 2
                assert($method instanceof ReflectionMethod);
65
                
66 2
                if (substr($method->getName(), 0, 3) === 'set') {
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
67 2
                    $propertyName = substr($method->getName(), 3);
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
68
                    
69 2
                    if ($propertyName === 'Icon') {
70 2
                        $topic = $this->parseIcon($topic, $relatedTopic->Icon);
71 2
                        continue;
72
                    }
73
                    
74 2
                    if (property_exists($relatedTopic, $propertyName)) {
75 2
                        $property = $relatedTopic->$propertyName;
76 2
                        $method->invoke($topic, $property);
77
                    }
78
                }
79
            }
80 2
            $result->addRelatedTopic($topic);
81
        }
82
        
83 2
        return $result;
84
    }
85
86 2
    private function parseIcon(RelatedTopic $topic, $icon): RelatedTopic
87
    {
88 2
        $ico = new Icon();
89
        
90 2
        $ref = new ReflectionClass($ico);
91 2
        $methods = $ref->getMethods(ReflectionMethod::IS_PUBLIC);
92 2 View Code Duplication
        foreach ($methods as $method) {
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...
93 2
            assert($method instanceof ReflectionMethod);
94
            
95 2
            if (substr($method->getName(), 0, 3) === 'set') {
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
96 2
                $propertyName = substr($method->getName(), 3);
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
97 2
                if (property_exists($icon, $propertyName)) {
98 2
                    $property = $icon->$propertyName;
99
                    
100 2
                    if ($propertyName === 'Height' || $propertyName === 'Width') {
101 2
                        $property = intval($property);
102
                    }
103 2
                    $method->invoke($ico, $property);
104
                }
105
            }
106
        }
107
        
108 2
        $topic->setIcon($ico);
109
        
110 2
        return $topic;
111
    }
112
113 2
    private function parseMeta(QueryResult $result, $meta): QueryResult
0 ignored issues
show
Unused Code introduced by
The parameter $meta is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
114
    {
115 2
        return $result;
116
    }
117
}