BalanceSheetPage::generateContent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace ByTIC\MFinante\Parsers;
4
5
use ByTIC\MFinante\Models\BalanceSheet;
6
use ByTIC\MFinante\Scrapers\BalanceSheetPage as Scraper;
7
use DOMElement;
8
9
/**
10
 * Class BalanceSheetPage
11
 * @package ByTIC\MFinante\Scrapers
12
 *
13
 * @method Scraper getScraper()
14
 */
15
class BalanceSheetPage extends AbstractParser
16
{
17
18
    /**
19
     * @inheritdoc
20
     */
21
    public function getModelClassName()
22
    {
23
        return BalanceSheet::class;
24
    }
25
26
    /**
27
     * @return array
28
     */
29 1
    protected function generateContent()
30
    {
31 1
        $return = [];
32 1
        $return = array_merge($return, $this->parseTable());
33 1
        return $return;
34
    }
35
36
    /**
37
     * @return array
38
     */
39 1 View Code Duplication
    protected function parseTable()
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...
40
    {
41 1
        $table = $this->getCrawler()->filter('#main > center > table > tbody')->first();
42 1
        $rows = $table->children();
43 1
        $return = [];
44 1
        foreach ($rows as $row) {
45 1
            $rowParsed = $this->parseTableRow($row);
46 1
            if ($rowParsed) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $rowParsed of type array 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...
47 1
                $return[$rowParsed[0]] = $rowParsed[1];
48
            }
49
        }
50 1
        return $return;
51
    }
52
53
    /**
54
     * @param DOMElement $row
55
     * @return array
56
     */
57 1 View Code Duplication
    protected function parseTableRow($row)
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...
58
    {
59 1
        $label = $row->childNodes[0]->nodeValue;
60 1
        $label = str_replace(':', '', $label);
61 1
        $label = str_replace('(*)', '', $label);
62 1
        $label = str_replace('(**)', '', $label);
63 1
        $label = str_replace("\n", ' ', $label);
64 1
        $label = str_replace("\t", ' ', $label);
65 1
        $label = preg_replace('/\s+/', ' ', $label);
66 1
        $label = trim($label);
67
68 1
        $value = str_replace("\n", ' ', $row->childNodes[2]->nodeValue);
69 1
        $value = preg_replace('/\s+/', ' ', $value);
70 1
        $value = trim($value);
71
72 1
        $labels = self::getLabelMaps($this->getScraper()->getYear());
73
74 1
        $labelFind = array_search($label, $labels);
75 1
        if ($labelFind) {
76 1
            return [$labelFind, $value];
77
        }
78 1
        return [];
79
    }
80
81
    /**
82
     * @param $year
83
     * @return array
84
     */
85 1
    public static function getLabelMaps($year)
86
    {
87
        $return = [
88 1
            'fixed_assets' => 'A. Active imobilizate - total',
89
            'current_assets' => 'B. Active circulante - total',
90
            'prepayments' => 'C. Cheltuieli in avans',
91
            'debts_year' => 'D. Datorii ce trebuie platite intr-o perioada de pana la un an',
92
            'net_current_assets' => 'E. Active circulante nete, respectiv datorii curente nete',
93
            'total_assets' => 'F. Total active minus datorii curente',
94
            'debts_year_plus' => 'G. Datorii ce trebuie platite intr-o perioada mai mare de un an',
95
            'provisions' => 'H. Provizioane',
96
            'income_advance' => 'I. Venituri in avans',
97
            'equity' => 'J. Capitaluri proprii - total',
98
            'funds_non_profit' => 'Fonduri privind activitatile fara scop patrimonial',
99
            'total_capital' => 'Capitaluri Total',
100
101
            'caen_non_profit' => 'CAEN privind activitatile fara scop patrimonial',
102
            'employees_non_profit' => 'Efectivul de personal privind activitatea fara scop patrimonial',
103
            'caen_economic' => 'CAEN privind activitatile economice sau financiare',
104
            'employees_economic' => 'Efectivul de personal privind activitatile economice sau financiare',
105
        ];
106
107
        $expenses = [
108 1
            'non_profit' => 'fara scop patrimonial',
109
            'special' => 'cu destinatie speciala',
110
            'economic' => 'economice',
111
            'total' => 'totale',
112
        ];
113
        $indicators = [
114 1
            'revenues' => 'Venituri',
115
            'expenses' => 'Cheltuieli',
116
            'surplus' => 'Excedent',
117
            'deficit' => 'Deficit',
118
        ];
119
120 1
        foreach ($expenses as $expenseKey => $expenseLabel) {
121 1
            if ($expenseKey == 'economic') {
122 1
                array_pop($indicators);
123 1
                array_pop($indicators);
124 1
                $indicators['profit'] = 'Profit';
125 1
                $indicators['loss'] = 'Pierdere';
126
            }
127 1
            foreach ($indicators as $indicatorKey => $indicatorLabel) {
128 1
                if ($expenseKey == 'total') {
129 1
                    if (in_array($indicatorKey, ['profit', 'loss'])) {
130 1
                        $indicatorLabel = ($indicatorKey == 'profit') ? 'Excedent/Profit' : 'Deficit/Pierdere';
131 1
                        $return['' . $indicatorKey . '_' . $expenseKey . '_provisions']
132 1
                            = $indicatorLabel . ' - prevederi anuale';
133 1
                        $return['' . $indicatorKey . '_' . $expenseKey . '_endyear']
134 1
                            = $indicatorLabel . ' - la 31.12.' . $year;
135
                    } else {
136 1
                        $return['' . $indicatorKey . '_' . $expenseKey . '_endyear']
137 1
                            = $indicatorLabel . ' ' . $expenseLabel . ' - la 31.12.' . $year;
138
139 1
                        $return['' . $indicatorKey . '_' . $expenseKey . '_provisions']
140 1
                            = $indicatorLabel . ' ' . $expenseLabel . ' - prevederi anuale';
141
                    }
142
                } else {
143 1
                    $linkAttribute = $indicatorKey == 'expenses' ? 'privind' : 'din';
144 1
                    $return['' . $indicatorKey . '_' . $expenseKey . '_provisions']
145 1
                        = $indicatorLabel . ' ' . $linkAttribute . ' activitatile ' . $expenseLabel . ' - prevederi anuale';
146
147 1
                    $return['' . $indicatorKey . '_' . $expenseKey . '_endyear']
148 1
                        = $indicatorLabel . ' ' . $linkAttribute . ' activitatile ' . $expenseLabel . ' - la 31.12.' . $year;
149
                }
150
            }
151
        }
152
153 1
        return $return;
154
    }
155
}
156