Failed Conditions
Push — master ( 029e81...ce5fbf )
by Luca
06:29
created

Csv::writeRow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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 Doctrine\Common\Collections\Collection;
14
use Ecodev\Felix\Api\Exception;
15
16
/**
17
 * Export multiples cards as CSV file
18
 */
19
class Csv implements Writer
20
{
21
    /**
22
     * @var resource
23
     */
24
    private $file;
25
26 1
    public function getExtension(): string
27
    {
28 1
        return 'csv';
29
    }
30
31 2
    public function initialize(Export $export, string $title): void
32
    {
33 2
        $file = fopen($export->getPath(), 'w+b');
34 2
        if ($file === false) {
35
            throw new Exception('Cannot write to file');
36
        }
37
38 2
        $this->file = $file;
39
40 2
        $this->headers();
41 2
    }
42
43 2
    public function finalize(): void
44
    {
45 2
        fclose($this->file);
46 2
    }
47
48 2
    private function writeRow(array $row): void
49
    {
50 2
        fputcsv($this->file, $row);
51 2
    }
52
53 2
    private function headers(): void
54
    {
55 2
        $this->writeRow([
56 2
            'Id',
57
            'Titre',
58
            'Titre étendu',
59
            'Domaine',
60
            'Période',
61
            'Date précise début',
62
            'Date précise fin',
63
            'Pays de découverte',
64
            'Site/Lieu de découverte',
65
            'Lieu de production',
66
            'Référence de l\'objet',
67
            'Type de document',
68
            'Auteur du document',
69
            'Année du document',
70
            'Latitude',
71
            'Longitude',
72
            'Précision',
73
        ]);
74 2
    }
75
76 2
    public function write(Card $card): void
77
    {
78 2
        $this->writeRow([
79 2
            $card->getId(),
80 2
            $card->getName(),
81 2
            $card->getExpandedName(),
82 2
            $this->collection($card->getDomains()),
83 2
            $this->collection($card->getPeriods()),
84 2
            $card->getFrom(),
85 2
            $card->getTo(),
86 2
            $this->nullableName($card->getCountry()),
87 2
            $card->getLocality(),
88 2
            $card->getProductionPlace(),
89 2
            $card->getObjectReference(),
90 2
            $this->nullableName($card->getDocumentType()),
91 2
            $card->getTechniqueAuthor(),
92 2
            $card->getTechniqueDate(),
93 2
            $card->getLatitude(),
94 2
            $card->getLongitude(),
95 2
            $card->getPrecision(),
96
        ]);
97 2
    }
98
99
    /**
100
     * @param null|Country|DocumentType $model
101
     */
102 2
    private function nullableName(?AbstractModel $model): string
103
    {
104 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

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