Browse Source

code beautifying

master
Evgeniy Ierusalimov 2 days ago
parent
commit
7972cf4fed

+ 1
- 2
src/Entity/PortfolioDetail.php View File

@@ -4,11 +4,10 @@ declare(strict_types=1);
4 4
 
5 5
 namespace App\Entity;
6 6
 
7
-use App\Repository\PortfolioDetailRepository;
8 7
 use Doctrine\DBAL\Types\Types;
9 8
 use Doctrine\ORM\Mapping as ORM;
10 9
 
11
-#[ORM\Entity(repositoryClass: PortfolioDetailRepository::class)]
10
+#[ORM\Entity]
12 11
 class PortfolioDetail
13 12
 {
14 13
     #[ORM\Id]

+ 1
- 2
src/Entity/PortfolioMovement.php View File

@@ -4,10 +4,9 @@ declare(strict_types=1);
4 4
 
5 5
 namespace App\Entity;
6 6
 
7
-use App\Repository\PortfolioMovementRepository;
8 7
 use Doctrine\ORM\Mapping as ORM;
9 8
 
10
-#[ORM\Entity(repositoryClass: PortfolioMovementRepository::class)]
9
+#[ORM\Entity]
11 10
 class PortfolioMovement
12 11
 {
13 12
     #[ORM\Id]

+ 63
- 27
src/Presentation/PortfolioPresenter.php View File

@@ -19,7 +19,8 @@ class PortfolioPresenter implements PortfolioPresenterInterface
19 19
     {
20 20
     }
21 21
 
22
-    public function toText(ParsedPortfolio $parsedPortfolio): string
22
+    /** @return array<int, string> */
23
+    public function toLines(ParsedPortfolio $parsedPortfolio): array
23 24
     {
24 25
         $header = $parsedPortfolio->header;
25 26
         $periodTitle = $header->startDate->format('d.m.y') . ' - ' . $header->endDate->format('d.m.y');
@@ -27,52 +28,87 @@ class PortfolioPresenter implements PortfolioPresenterInterface
27 28
         $total = $this->calcTotalSum($parsedPortfolio);
28 29
 
29 30
         $output = new BufferedOutput();
31
+        $cellStyle = $this->createCellStyle();
30 32
 
31
-        $tableStyle = new TableStyle();
32
-        $tableStyle->setPadType(STR_PAD_LEFT);
33
-
34
-        $cellStyle = new TableStyle();
35
-        $cellStyle->setPadType(STR_PAD_RIGHT);
36
-
37
-        $table = new Table($output);
38
-        $table->setHeaderTitle('ПОРТФЕЛЬ: ' . $periodTitle);
39
-        $table->setHeaders(['ЦБ', 'Кол-во', 'Цн', 'Цк', 'Сумм', '%']);
40
-
33
+        $portfolioRows = [];
41 34
         foreach ($parsedPortfolio->details as $detail) {
42
-            $table->addRow([
35
+            $portfolioRows[] = [
43 36
                 $this->titleResolver->resolve($detail->security, $detail->issuer),
44 37
                 $this->formatQuantity($detail->quantityStart, $detail->quantityEnd),
45 38
                 $detail->priceStart,
46 39
                 $detail->priceEnd,
47 40
                 round((float) $detail->sumTotal),
48 41
                 round(100 * (float) $detail->sumTotal / $total, 2),
49
-            ]);
42
+            ];
50 43
         }
44
+        $lines = $this->renderTable($output, 'ПОРТФЕЛЬ: ' . $periodTitle,
45
+            ['ЦБ', 'Кол-во', 'Цн', 'Цк', 'Сумм', '%'],
46
+            $portfolioRows, $cellStyle,
47
+        );
51 48
 
52
-        $table->setColumnStyle(0, $cellStyle);
53
-        $table->setStyle($tableStyle)->render();
54
-
55
-        $txt = $output->fetch();
56
-
57
-        $table = new Table($output);
58
-        $table->setHeaderTitle('ДВИЖ: ' . $periodTitle);
59
-        $table->setHeaders(['ЦБ', 'Кн', 'In', 'Out', 'Кк']);
60
-
49
+        $movementRows = [];
61 50
         foreach ($parsedPortfolio->movements as $movement) {
62
-            $table->addRow([
51
+            $movementRows[] = [
63 52
                 $this->titleResolver->resolve($movement->security),
64 53
                 $movement->quantityStart,
65 54
                 $movement->quantityIncome > 0 ? '+' . $movement->quantityIncome : $movement->quantityIncome,
66 55
                 $movement->quantityOutcome > 0 ? '-' . $movement->quantityOutcome : $movement->quantityOutcome,
67 56
                 $movement->quantityEnd,
68
-            ]);
57
+            ];
58
+        }
59
+        $mvLines = $this->renderTable($output, 'ДВИЖ: ' . $periodTitle,
60
+            ['ЦБ', 'Кн', 'In', 'Out', 'Кк'],
61
+            $movementRows, $cellStyle,
62
+        );
63
+
64
+        $lines[] = '';
65
+        foreach ($mvLines as $line) {
66
+            $lines[] = $line;
67
+        }
68
+        $lines[count($lines) - 1] = rtrim($lines[count($lines) - 1]) . self::COPYRIGHT_MARK;
69
+
70
+        return $lines;
71
+    }
72
+
73
+    public function toText(ParsedPortfolio $parsedPortfolio): string
74
+    {
75
+        return implode("\n", $this->toLines($parsedPortfolio));
76
+    }
77
+
78
+    /**
79
+     * @param array<int, array<string|int>> $rows
80
+     * @return array<int, string>
81
+     */
82
+    private function renderTable(
83
+        BufferedOutput $output,
84
+        string $title,
85
+        array $headers,
86
+        array $rows,
87
+        TableStyle $cellStyle,
88
+    ): array {
89
+        $table = new Table($output);
90
+        $table->setHeaderTitle($title);
91
+        $table->setHeaders($headers);
92
+
93
+        foreach ($rows as $row) {
94
+            $table->addRow($row);
69 95
         }
70 96
 
71 97
         $table->setColumnStyle(0, $cellStyle);
72
-        $table->setStyle($tableStyle)->render();
73
-        $txt .= PHP_EOL . rtrim($output->fetch()) . self::COPYRIGHT_MARK;
74 98
 
75
-        return $txt;
99
+        $padStyle = new TableStyle();
100
+        $padStyle->setPadType(STR_PAD_LEFT);
101
+        $table->setStyle($padStyle)->render();
102
+
103
+        return explode("\n", rtrim($output->fetch()));
104
+    }
105
+
106
+    private function createCellStyle(): TableStyle
107
+    {
108
+        $style = new TableStyle();
109
+        $style->setPadType(STR_PAD_RIGHT);
110
+
111
+        return $style;
76 112
     }
77 113
 
78 114
     private function calcTotalSum(ParsedPortfolio $parsedPortfolio): float

+ 0
- 20
src/Repository/PortfolioDetailRepository.php View File

@@ -1,20 +0,0 @@
1
-<?php
2
-
3
-declare(strict_types=1);
4
-
5
-namespace App\Repository;
6
-
7
-use App\Entity\PortfolioDetail;
8
-use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
9
-use Doctrine\Persistence\ManagerRegistry;
10
-
11
-/**
12
- * @extends ServiceEntityRepository<PortfolioDetail>
13
- */
14
-class PortfolioDetailRepository extends ServiceEntityRepository
15
-{
16
-    public function __construct(ManagerRegistry $registry)
17
-    {
18
-        parent::__construct($registry, PortfolioDetail::class);
19
-    }
20
-}

+ 0
- 20
src/Repository/PortfolioMovementRepository.php View File

@@ -1,20 +0,0 @@
1
-<?php
2
-
3
-declare(strict_types=1);
4
-
5
-namespace App\Repository;
6
-
7
-use App\Entity\PortfolioMovement;
8
-use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
9
-use Doctrine\Persistence\ManagerRegistry;
10
-
11
-/**
12
- * @extends ServiceEntityRepository<PortfolioMovement>
13
- */
14
-class PortfolioMovementRepository extends ServiceEntityRepository
15
-{
16
-    public function __construct(ManagerRegistry $registry)
17
-    {
18
-        parent::__construct($registry, PortfolioMovement::class);
19
-    }
20
-}

+ 1
- 1
src/Service/PortfolioImageRenderer.php View File

@@ -27,7 +27,7 @@ readonly class PortfolioImageRenderer
27 27
         $grey = imagecolorallocate($im, 48, 48, 48);
28 28
         imagefill($im, 0, 0, $grey);
29 29
 
30
-        $lines = explode("\n", $this->presenter->toText($parsedPortfolio));
30
+        $lines = $this->presenter->toLines($parsedPortfolio);
31 31
 
32 32
         $height = self::FONT_SIZE;
33 33
         foreach ($lines as $line) {

+ 3
- 0
src/Service/PortfolioPresenterInterface.php View File

@@ -8,5 +8,8 @@ use App\Domain\Entity\ParsedPortfolio;
8 8
 
9 9
 interface PortfolioPresenterInterface
10 10
 {
11
+    /** @return array<int, string> */
12
+    public function toLines(ParsedPortfolio $parsedPortfolio): array;
13
+
11 14
     public function toText(ParsedPortfolio $parsedPortfolio): string;
12 15
 }

+ 7
- 11
src/Service/SecurityTitleResolver.php View File

@@ -6,8 +6,8 @@ namespace App\Service;
6 6
 
7 7
 final class SecurityTitleResolver
8 8
 {
9
-    /** @var array{list<string>, list<string>} */
10
-    private readonly array $mapPrintable;
9
+    /** @var array<string, string> */
10
+    private readonly array $replacements;
11 11
 
12 12
     /** @var array<string, string> */
13 13
     private array $cache;
@@ -15,19 +15,15 @@ final class SecurityTitleResolver
15 15
     public function __construct(array $securitiesMap)
16 16
     {
17 17
         $this->cache = [];
18
-        $this->mapPrintable = [
19
-            array_merge(['(в пути)'], array_keys($securitiesMap)),
20
-            array_merge(['±'], array_values($securitiesMap)),
21
-        ];
18
+        $this->replacements = array_merge(
19
+            ['(в пути)' => '±'],
20
+            $securitiesMap,
21
+        );
22 22
     }
23 23
 
24 24
     public function resolve(string $security, ?string $issuer = null): string
25 25
     {
26
-        $security = str_replace(
27
-            $this->mapPrintable[0],
28
-            $this->mapPrintable[1],
29
-            $security,
30
-        );
26
+        $security = strtr($security, $this->replacements);
31 27
 
32 28
         if (!str_starts_with($security, 'RU00')) {
33 29
             return $security;

+ 29
- 37
src/Service/XmlParser.php View File

@@ -95,33 +95,18 @@ class XmlParser implements XmlParserInterface
95 95
     {
96 96
         $items = [];
97 97
 
98
-        foreach (get_object_vars($xml) as $key => $value) {
99
-            if (mb_strpos($key, self::PREFIX_DETAILS) === false) {
100
-                continue;
101
-            }
102
-
103
-            if (!is_array($value)) {
104
-                $value = [$value];
105
-            }
106
-
107
-            foreach ($value as $line) {
108
-                $attr = [];
109
-                foreach ($line->attributes() as $attrName => $attrValue) {
110
-                    $attr[$attrName] = get_object_vars($attrValue)[0];
111
-                }
112
-
113
-                $items[] = new PortfolioDetailItem(
114
-                    issuer:        $attr[self::ATTR_ISSUER],
115
-                    security:      $attr[self::ATTR_SECURITY],
116
-                    priceStart:    $attr[self::ATTR_PRICE_START],
117
-                    priceEnd:      $attr[self::ATTR_PRICE_END],
118
-                    quantityStart: (int) $attr[self::ATTR_QTY_START],
119
-                    quantityEnd:   (int) $attr[self::ATTR_QTY_END],
120
-                    sumStart:      $attr[self::ATTR_SUM_START],
121
-                    sumEnd:        $attr[self::ATTR_SUM_END],
122
-                    sumTotal:      $attr[self::ATTR_SUM_TOTAL],
123
-                );
124
-            }
98
+        foreach ($this->iterateDetailElements($xml) as $attr) {
99
+            $items[] = new PortfolioDetailItem(
100
+                issuer:        $attr[self::ATTR_ISSUER],
101
+                security:      $attr[self::ATTR_SECURITY],
102
+                priceStart:    $attr[self::ATTR_PRICE_START],
103
+                priceEnd:      $attr[self::ATTR_PRICE_END],
104
+                quantityStart: (int) $attr[self::ATTR_QTY_START],
105
+                quantityEnd:   (int) $attr[self::ATTR_QTY_END],
106
+                sumStart:      $attr[self::ATTR_SUM_START],
107
+                sumEnd:        $attr[self::ATTR_SUM_END],
108
+                sumTotal:      $attr[self::ATTR_SUM_TOTAL],
109
+            );
125 110
         }
126 111
 
127 112
         return $items;
@@ -131,6 +116,22 @@ class XmlParser implements XmlParserInterface
131 116
     {
132 117
         $items = [];
133 118
 
119
+        foreach ($this->iterateDetailElements($xml) as $attr) {
120
+            $items[] = new PortfolioMovementItem(
121
+                security:       $attr[self::ATTR_SECURITY],
122
+                period:         new \DateTimeImmutable($attr[self::ATTR_PERIOD]),
123
+                quantityStart:  (int) $attr[self::ATTR_QTY_START_MV],
124
+                quantityEnd:    (int) $attr[self::ATTR_QTY_END_MV],
125
+                quantityIncome: (int) $attr[self::ATTR_QTY_INCOME],
126
+                quantityOutcome:(int) $attr[self::ATTR_QTY_OUTCOME],
127
+            );
128
+        }
129
+
130
+        return $items;
131
+    }
132
+
133
+    private function iterateDetailElements(\SimpleXMLElement $xml): iterable
134
+    {
134 135
         foreach (get_object_vars($xml) as $key => $value) {
135 136
             if (mb_strpos($key, self::PREFIX_DETAILS) === false) {
136 137
                 continue;
@@ -146,17 +147,8 @@ class XmlParser implements XmlParserInterface
146 147
                     $attr[$attrName] = get_object_vars($attrValue)[0];
147 148
                 }
148 149
 
149
-                $items[] = new PortfolioMovementItem(
150
-                    security:       $attr[self::ATTR_SECURITY],
151
-                    period:         new \DateTimeImmutable($attr[self::ATTR_PERIOD]),
152
-                    quantityStart:  (int) $attr[self::ATTR_QTY_START_MV],
153
-                    quantityEnd:    (int) $attr[self::ATTR_QTY_END_MV],
154
-                    quantityIncome: (int) $attr[self::ATTR_QTY_INCOME],
155
-                    quantityOutcome:(int) $attr[self::ATTR_QTY_OUTCOME],
156
-                );
150
+                yield $attr;
157 151
             }
158 152
         }
159
-
160
-        return $items;
161 153
     }
162 154
 }

Loading…
Cancel
Save