Csv   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 96.83%

Importance

Changes 0
Metric Value
wmc 11
eloc 56
dl 0
loc 103
ccs 61
cts 63
cp 0.9683
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A write() 0 20 1
A getExtension() 0 3 1
A headers() 0 20 1
A initialize() 0 10 2
A writeRow() 0 3 1
A collection() 0 15 2
A finalize() 0 3 1
A nullableName() 0 3 2
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