Passed
Branch feature/2.1-geodispersion-dev (1d61a8)
by Jonathan
61:21
created

Certificate::fetchALinkedSource()   C

Complexity

Conditions 13
Paths 12

Size

Total Lines 66
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 48
c 1
b 0
f 0
nc 12
nop 0
dl 0
loc 66
rs 6.6166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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-2021, 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
    /**
36
     * @var Tree $tree
37
     */
38
    private $tree;
39
40
    /**
41
     * @var string $path
42
     * */
43
    private $path;
44
45
    /**
46
     * @var string|null $city
47
     * $city */
48
    private $city;
49
50
    /**
51
     * @var string|null $filename
52
     */
53
    private $filename;
54
55
    /**
56
     * @var string|null $extension
57
     */
58
    private $extension;
59
60
    /**
61
     * @var string|null $type
62
     */
63
    private $type;
64
65
    /**
66
     * @var string|null $description
67
     */
68
    private $description;
69
70
    /**
71
     * @var Date|null $date
72
     */
73
    private $date;
74
75
    /**
76
     * Contructor for Certificate
77
     *
78
     * @param Tree $tree
79
     * @param string $path
80
     */
81
    public function __construct(Tree $tree, string $path)
82
    {
83
        $this->tree = $tree;
84
        $this->path = $path;
85
        $this->extractDataFromPath($path);
86
    }
87
88
    /**
89
     * Populate fields from the filename, based on a predeterminate pattern.
90
     * Logic specific to the author.
91
     *
92
     * @param string $path
93
     */
94
    protected function extractDataFromPath(string $path): void
95
    {
96
        $path_parts = pathinfo($path);
97
        $this->city = $path_parts['dirname'];
98
        $this->filename = $path_parts['filename'];
99
        $this->extension = $path_parts['extension'] ?? '';
100
101
        if (preg_match(self::FILENAME_PATTERN, $this->filename, $match) === 1) {
102
            $this->type = $match['type'];
103
            $this->description = $match['descr'];
104
105
            $day = $match['day'] ?? '';
106
            $month_date = DateTime::createFromFormat('m', $match['month'] ?? '');
107
            $month = $month_date !== false ? strtoupper($month_date->format('M')) : '';
108
            $year = $match['year'] ?? '';
109
110
            $this->date = new Date(sprintf('%s %s %s', $day, $month, $year));
111
        } else {
112
            $this->description = $this->filename;
113
        }
114
    }
115
116
    /**
117
     * Get the family tree of the certificate
118
     *
119
     * @return Tree
120
     */
121
    public function tree(): Tree
122
    {
123
        return $this->tree;
124
    }
125
126
    /**
127
     * Get the path of the certificate in the file system.
128
     *
129
     * @return string
130
     */
131
    public function path(): string
132
    {
133
        return $this->path;
134
    }
135
136
    /**
137
     * The the path of the certificate, in a Gedcom canonical form.
138
     *
139
     * @return string
140
     */
141
    public function gedcomPath(): string
142
    {
143
        return str_replace('\\', '/', $this->path);
144
    }
145
146
    /**
147
     * Get the certificate name.
148
     *
149
     * @return string
150
     */
151
    public function name(): string
152
    {
153
        return $this->filename ?? '';
154
    }
155
156
    /**
157
     * Get the certificate's city (the first level folder).
158
     *
159
     * @return string
160
     */
161
    public function city(): string
162
    {
163
        return $this->city ?? '';
164
    }
165
166
    /**
167
     * Get the certificate's date. Extracted from the file name.
168
     *
169
     * @return Date
170
     */
171
    public function date(): Date
172
    {
173
        return $this->date ?? new Date('');
174
    }
175
176
    /**
177
     * Get the certificate's type. Extracted from the file name.
178
     *
179
     * @return string
180
     */
181
    public function type(): string
182
    {
183
        return $this->type ?? '';
184
    }
185
186
    /**
187
     * Get the certificate's description.  Extracted from the file name.
188
     * @return string
189
     */
190
    public function description(): string
191
    {
192
        return $this->description ?? '';
193
    }
194
195
    /**
196
     * Get the certificate's description to be used for sorting.
197
     * This is based on surnames (at least 3 letters) found in the file name.
198
     *
199
     * @return string
200
     */
201
    public function sortDescription(): string
202
    {
203
        $sort_prefix = '';
204
        if (preg_match_all('/\b([A-Z]{3,})\b/', $this->description(), $matches, PREG_SET_ORDER) >= 1) {
205
            $sort_prefix = implode('_', array_map(function ($match) {
206
                return $match[1];
207
            }, $matches)) . '_';
208
        }
209
        return $sort_prefix . $this->description();
210
    }
211
212
    /**
213
     * Get the certificate's MIME type.
214
     *
215
     * @return string
216
     */
217
    public function mimeType(): string
218
    {
219
        return Mime::TYPES[$this->extension] ?? Mime::DEFAULT_TYPE;
220
    }
221
222
    /**
223
     * Get the base parameters to be used in url referencing the certificate.
224
     *
225
     * @param UrlObfuscatorService $url_obfuscator_service
226
     * @return array
227
     */
228
    public function urlParameters(UrlObfuscatorService $url_obfuscator_service = null): array
229
    {
230
        $url_obfuscator_service = $url_obfuscator_service ?? app(UrlObfuscatorService::class);
231
        return [
232
            'tree' => $this->tree->name(),
233
            'cid' => $url_obfuscator_service->obfuscate($this->path)
234
        ];
235
    }
236
}
237