Completed
Push — master ( b12e0e...67c7aa )
by Stojan
01:54
created

HtmlParser   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 158
Duplicated Lines 20.25 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 20
c 2
b 0
f 0
lcom 1
cbo 2
dl 32
loc 158
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getRates() 0 8 2
A parseHtml() 0 7 1
B extractRates() 32 57 6
D parseRow() 0 35 9
A buildRate() 0 13 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 * This file is part of the Exchange Rate package, an RunOpenCode project.
4
 *
5
 * Implementation of exchange rate crawler for Banca Intesa Serbia, http://www.bancaintesa.rs.
6
 *
7
 * (c) 2016 RunOpenCode
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
namespace RunOpenCode\ExchangeRate\BancaIntesaSerbia\Parser;
13
14
use RunOpenCode\ExchangeRate\Contract\RateInterface;
15
use RunOpenCode\ExchangeRate\Model\Rate;
16
use RunOpenCode\ExchangeRate\BancaIntesaSerbia\Api;
17
use Symfony\Component\DomCrawler\Crawler;
18
19
/**
20
 * Class HtmlParser
21
 *
22
 * Parse HTML document with daily rates from Banca Intesa Serbia.
23
 *
24
 * @package RunOpenCode\ExchangeRate\BancaIntesaSerbia\Parser
25
 */
26
class HtmlParser
27
{
28
    /**
29
     * @var RateInterface[]
30
     */
31
    private $rates;
32
33
    /**
34
     * @var array
35
     */
36
    private $currentRate;
0 ignored issues
show
Unused Code introduced by
The property $currentRate is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
37
38
    /**
39
     * @var \DateTime
40
     */
41
    private $date;
42
43
    /**
44
     * @var string
45
     */
46
    private $html;
47
48
    /**
49
     * HtmlParser constructor.
50
     * @param $node string
51
     * @param $date \DateTime
52
     */
53
    public function __construct($node, \DateTime $date)
54
    {
55
        $this->html = $node;
56
        $this->date = $date;
57
    }
58
59
    public function getRates()
60
    {
61
        if (!$this->rates) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->rates of type RunOpenCode\ExchangeRate\Contract\RateInterface[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
62
            $this->rates = $this->parseHtml($this->html, $this->date);
63
        }
64
65
        return $this->rates;
66
    }
67
68
    private function parseHtml($html, \DateTime $date)
69
    {
70
        $crawler = new Crawler($html);
71
        $crawler = $crawler->filter('table');
72
73
        return $this->extractRates($crawler, $date);
74
    }
75
76
    private function extractRates(Crawler $crawler, \DateTime $date)
77
    {
78
        $rates = array();
79
80
        $crawler->filter('tr')->each(function (Crawler $node) use ($date, &$rates) {
81
82
            $row = $this->parseRow($node);
83
84
            if (null !== $row) {
85
86
                $rates[] = $this->buildRate(
87
                    $row['default'] / $row['unit'],
88
                    $row['currencyCode'],
89
                    'default',
90
                    $date
91
                );
92
93 View Code Duplication
                if ($row['foreign_exchange_buying'] > 0) {
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
                    $rates[] = $this->buildRate(
95
                        $row['foreign_exchange_buying'] / $row['unit'],
96
                        $row['currencyCode'],
97
                        'foreign_exchange_buying',
98
                        $date
99
                    );
100
                }
101
102 View Code Duplication
                if ($row['foreign_exchange_selling'] > 0) {
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...
103
                    $rates[] = $this->buildRate(
104
                        $row['foreign_exchange_selling'] / $row['unit'],
105
                        $row['currencyCode'],
106
                        'foreign_exchange_selling',
107
                        $this->date
108
                    );
109
                }
110
111 View Code Duplication
                if ($row['foreign_cash_buying'] > 0) {
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...
112
                    $rates[] = $this->buildRate(
113
                        $row['foreign_cash_buying'] / $row['unit'],
114
                        $row['currencyCode'],
115
                        'foreign_cash_buying',
116
                        $this->date
117
                    );
118
                }
119
120 View Code Duplication
                if ($row['foreign_cash_selling'] > 0) {
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...
121
                    $rates[] = $this->buildRate(
122
                        $row['foreign_cash_selling'] / $row['unit'],
123
                        $row['currencyCode'],
124
                        'foreign_cash_selling',
125
                        $this->date
126
                    );
127
                }
128
            }
129
        });
130
131
        return $rates;
132
    }
133
134
    private function parseRow(Crawler $crawler)
135
    {
136
        $currentRow = array(
137
            'currencyCode' => ''
138
        );
139
140
        $crawler->filter('td')->each(function (Crawler $node, $i) use (&$currentRow) {
141
142
            switch ($i) {
143
                case 1:
144
                    $currentRow['currencyCode'] = trim($node->text());
145
                    break;
146
                case 2:
147
                    $currentRow['unit'] = (int)trim($node->text());
148
                    break;
149
                case 3:
150
                    $currentRow['foreign_exchange_buying'] = (float)trim($node->text());
151
                    break;
152
                case 4:
153
                    $currentRow['default'] = (float)trim($node->text());
154
                    break;
155
                case 5:
156
                    $currentRow['foreign_exchange_selling'] = (float)trim($node->text());
157
                    break;
158
                case 6:
159
                    $currentRow['foreign_cash_buying'] = (float)trim($node->text());
160
                    break;
161
                case 7:
162
                    $currentRow['foreign_cash_selling'] = (float)trim($node->text());
163
                    break;
164
            }
165
        });
166
167
        return strlen($currentRow['currencyCode']) === 3 ? $currentRow : null;
168
    }
169
170
    private function buildRate($value, $currencyCode, $rateType, $date) {
171
172
        return new Rate(
173
            Api::NAME,
174
            $value,
175
            $currencyCode,
176
            $rateType,
177
            $date,
178
            'RSD',
179
            new \DateTime('now'),
180
            new \DateTime('now')
181
        );
182
    }
183
}
184