Csv::initialize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.0185
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Service\Exporter;
6
7
use Application\Model\AbstractModel;
8
use Application\Model\Card;
9
use Application\Model\Country;
10
use Application\Model\DocumentType;
11
use Application\Model\Export;
12
use Application\Traits\HasParentInterface;
13
use Application\Utility;
14
use Doctrine\Common\Collections\Collection;
15
use Ecodev\Felix\Api\Exception;
16
17
/**
18
 * Export multiples cards as CSV file.
19
 */
20
class Csv implements Writer
21
{
22
    /**
23
     * @var resource
24
     */
25
    private $file;
26
27 1
    public function getExtension(): string
28
    {
29 1
        return 'csv';
30
    }
31
32 2
    public function initialize(Export $export, string $title): void
33
    {
34 2
        $file = fopen($export->getPath(), 'w+b');
35 2
        if ($file === false) {
36
            throw new Exception('Cannot write to file');
37
        }
38
39 2
        $this->file = $file;
40
41 2
        $this->headers();
42
    }
43
44 2
    public function finalize(): void
45
    {
46 2
        fclose($this->file);
47
    }
48
49 2
    private function writeRow(array $row): void
50
    {
51 2
        fputcsv($this->file, $row);
52
    }
53
54 2
    private function headers(): void
55
    {
56 2
        $this->writeRow([
57 2
            'Id',
58 2
            'Titre',
59 2
            'Titre étendu',
60 2
            'Domaine',
61 2
            'Période',
62 2
            'Date précise début',
63 2
            'Date précise fin',
64 2
            'Pays de découverte',
65 2
            'Site/Lieu de découverte',
66 2
            'Lieu de production',
67 2
            'Référence de l\'objet',
68 2
            'Type de document',
69 2
            'Auteur du document',
70 2
            'Année du document',
71 2
            'Latitude',
72 2
            'Longitude',
73 2
            'Précision',
74 2
        ]);
75
    }
76
77 2
    public function write(Card $card): void
78
    {
79 2
        $this->writeRow([
80 2
            $card->getId(),
81 2
            Utility::richTextToPlainText($card->getName()),
82 2
            Utility::richTextToPlainText($card->getExpandedName()),
83 2
            $this->collection($card->getDomains()),
84 2
            $this->collection($card->getPeriods()),
85 2
            $card->getFrom(),
86 2
            $card->getTo(),
87 2
            $this->nullableName($card->getCountry()),
88 2
            $card->getLocality(),
89 2
            $card->getProductionPlace(),
90 2
            $card->getObjectReference(),
91 2
            $this->nullableName($card->getDocumentType()),
92 2
            $card->getTechniqueAuthor(),
93 2
            $card->getTechniqueDate(),
94 2
            $card->getLatitude(),
95 2
            $card->getLongitude(),
96 2
            $card->getPrecision()?->value,
97 2
        ]);
98
    }
99
100
    /**
101
     * @param null|Country|DocumentType $model
102
     */
103 2
    private function nullableName(?AbstractModel $model): string
104
    {
105 2
        return $model ? $model->getName() : '';
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Application\Model\AbstractModel. It seems like you code against a sub-type of said class. However, the method does not exist in Application\Model\Statistic or Application\Model\Export or Application\Model\Change or Application\Model\Dating or Application\Model\Log or Application\Model\Message. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
        return $model ? $model->/** @scrutinizer ignore-call */ getName() : '';
Loading history...
106
    }
107
108 2
    private function collection(Collection $collection): string
109
    {
110 2
        $lines = $collection->map(function ($model) {
111 1
            if ($model instanceof HasParentInterface) {
112 1
                $name = $model->getHierarchicName();
113
            } else {
114
                $name = $model->getName();
115
            }
116
117 1
            return '• ' . $name;
118 2
        })->toArray();
119
120 2
        sort($lines);
121
122 2
        return implode(PHP_EOL, $lines);
123
    }
124
}
125