Certificate::description()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees-lib: MyArtJaub library for webtrees
5
 *
6
 * @package MyArtJaub\Webtrees
7
 * @subpackage Certificates
8
 * @author Jonathan Jaubart <[email protected]>
9
 * @copyright Copyright (c) 2009-2022, Jonathan Jaubart
10
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
11
 */
12
13
declare(strict_types=1);
14
15
namespace MyArtJaub\Webtrees\Module\Certificates\Model;
16
17
use Fisharebest\Webtrees\Date;
18
use Fisharebest\Webtrees\Mime;
19
use Fisharebest\Webtrees\Tree;
20
use MyArtJaub\Webtrees\Module\Certificates\Services\UrlObfuscatorService;
21
use DateTime;
22
23
/**
24
 * Model class for a certificate file.
25
 */
26
class Certificate
27
{
28
    /**
29
     * Pattern to extract information from a file name.
30
     * Specific to the author's workflow.
31
     * @var string
32
     */
33
    private const FILENAME_PATTERN = '/^(?<year>\d{1,4})(\.(?<month>\d{1,2}))?(\.(?<day>\d{1,2}))?( (?<type>[A-Z]{1,2}))?\s(?<descr>.*)/'; //phpcs:ignore Generic.Files.LineLength.TooLong
34
35
    private Tree $tree;
36
    private string $path;
37
    private ?string $city = null;
38
    private ?string $basename = null;
39
    private ?string $filename = null;
40
    private ?string $extension = null;
41
    private ?string $type = null;
42
    private ?string $description = null;
43
    private ?Date $date = null;
44
45
    /**
46
     * Contructor for Certificate
47
     *
48
     * @param Tree $tree
49
     * @param string $path
50
     */
51
    public function __construct(Tree $tree, string $path)
52
    {
53
        $this->tree = $tree;
54
        $this->path = $path;
55
        $this->extractDataFromPath($path);
56
    }
57
58
    /**
59
     * Populate fields from the filename, based on a predeterminate pattern.
60
     * Logic specific to the author.
61
     *
62
     * @param string $path
63
     */
64
    protected function extractDataFromPath(string $path): void
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed. ( Ignorable by Annotation )

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

64
    protected function extractDataFromPath(/** @scrutinizer ignore-unused */ string $path): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
65
    {
66
        $path_parts = pathinfo($this->gedcomPath());
67
        $this->city = $path_parts['dirname'] ?? '';
68
        $this->basename = $path_parts['basename'];
69
        $this->filename = $path_parts['filename'];
70
        $this->extension = strtoupper($path_parts['extension'] ?? '');
71
72
        if (preg_match(self::FILENAME_PATTERN, $this->filename, $match) === 1) {
0 ignored issues
show
Bug introduced by
It seems like $this->filename can also be of type null; however, parameter $subject of preg_match() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

72
        if (preg_match(self::FILENAME_PATTERN, /** @scrutinizer ignore-type */ $this->filename, $match) === 1) {
Loading history...
73
            $this->type = $match['type'];
74
            $this->description = $match['descr'];
75
76
            $day = $match['day'] ?? '';
77
            $month_date = DateTime::createFromFormat('m', $match['month'] ?? '');
78
            $month = $month_date !== false ? strtoupper($month_date->format('M')) : '';
79
            $year = $match['year'] ?? '';
80
81
            $this->date = new Date(sprintf('%s %s %s', $day, $month, $year));
82
        } else {
83
            $this->description = $this->filename;
84
        }
85
    }
86
87
    /**
88
     * Get the family tree of the certificate
89
     *
90
     * @return Tree
91
     */
92
    public function tree(): Tree
93
    {
94
        return $this->tree;
95
    }
96
97
    /**
98
     * Get the path of the certificate in the file system.
99
     *
100
     * @return string
101
     */
102
    public function path(): string
103
    {
104
        return $this->path;
105
    }
106
107
    /**
108
     * The the path of the certificate, in a Gedcom canonical form.
109
     *
110
     * @return string
111
     */
112
    public function gedcomPath(): string
113
    {
114
        return str_replace('\\', '/', $this->path);
115
    }
116
117
    /**
118
     * Get the certificate name.
119
     *
120
     * @return string
121
     */
122
    public function name(): string
123
    {
124
        return $this->filename ?? '';
125
    }
126
127
    /**
128
     * Get the certificate file name.
129
     *
130
     * @return string
131
     */
132
    public function filename(): string
133
    {
134
        return $this->basename ?? '';
135
    }
136
137
    /**
138
     * Get the certificate's city (the first level folder).
139
     *
140
     * @return string
141
     */
142
    public function city(): string
143
    {
144
        return $this->city ?? '';
145
    }
146
147
    /**
148
     * Get the certificate's date. Extracted from the file name.
149
     *
150
     * @return Date
151
     */
152
    public function date(): Date
153
    {
154
        return $this->date ?? new Date('');
155
    }
156
157
    /**
158
     * Get the certificate's type. Extracted from the file name.
159
     *
160
     * @return string
161
     */
162
    public function type(): string
163
    {
164
        return $this->type ?? '';
165
    }
166
167
    /**
168
     * Get the certificate's description.  Extracted from the file name.
169
     * @return string
170
     */
171
    public function description(): string
172
    {
173
        return $this->description ?? '';
174
    }
175
176
    /**
177
     * Get the certificate's description to be used for sorting.
178
     * This is based on surnames (at least 3 letters) found in the file name.
179
     *
180
     * @return string
181
     */
182
    public function sortDescription(): string
183
    {
184
        $sort_prefix = '';
185
        if (preg_match_all('/\b([A-Z]{3,})\b/', $this->description(), $matches, PREG_SET_ORDER) >= 1) {
186
            $sort_prefix = implode('_', array_map(function ($match) {
187
                return $match[1];
188
            }, $matches)) . '_';
189
        }
190
        return $sort_prefix . $this->description();
191
    }
192
193
    /**
194
     * Get the certificate's MIME type.
195
     *
196
     * @return string
197
     */
198
    public function mimeType(): string
199
    {
200
        return Mime::TYPES[$this->extension] ?? Mime::DEFAULT_TYPE;
201
    }
202
203
    /**
204
     * Get the base parameters to be used in url referencing the certificate.
205
     *
206
     * @param UrlObfuscatorService $url_obfuscator_service
207
     * @return array{tree: string, cid: mixed}
208
     */
209
    public function urlParameters(UrlObfuscatorService $url_obfuscator_service = null): array
210
    {
211
        $url_obfuscator_service = $url_obfuscator_service ?? app(UrlObfuscatorService::class);
212
        return [
213
            'tree' => $this->tree->name(),
214
            'cid' => $url_obfuscator_service->obfuscate($this->path)
215
        ];
216
    }
217
}
218