Passed
Push — main ( 9fcb9f...c808ff )
by Jonathan
04:18
created
app/Module/Certificates/Http/RequestHandlers/CertificatesList.php 1 patch
Indentation   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -32,63 +32,63 @@
 block discarded – undo
32 32
  */
33 33
 class CertificatesList implements RequestHandlerInterface
34 34
 {
35
-    use ViewResponseTrait;
35
+	use ViewResponseTrait;
36 36
 
37
-    private ?CertificatesModule $module;
38
-    private CertificateFilesystemService $certif_filesystem;
39
-    private UrlObfuscatorService $url_obfuscator_service;
37
+	private ?CertificatesModule $module;
38
+	private CertificateFilesystemService $certif_filesystem;
39
+	private UrlObfuscatorService $url_obfuscator_service;
40 40
 
41
-    /**
42
-     * Constructor for CertificatesList Request Handler
43
-     *
44
-     * @param ModuleService $module_service
45
-     */
46
-    public function __construct(
47
-        ModuleService $module_service,
48
-        CertificateFilesystemService $certif_filesystem,
49
-        UrlObfuscatorService $url_obfuscator_service
50
-    ) {
51
-        $this->module = $module_service->findByInterface(CertificatesModule::class)->first();
52
-        $this->certif_filesystem = $certif_filesystem;
53
-        $this->url_obfuscator_service = $url_obfuscator_service;
54
-    }
41
+	/**
42
+	 * Constructor for CertificatesList Request Handler
43
+	 *
44
+	 * @param ModuleService $module_service
45
+	 */
46
+	public function __construct(
47
+		ModuleService $module_service,
48
+		CertificateFilesystemService $certif_filesystem,
49
+		UrlObfuscatorService $url_obfuscator_service
50
+	) {
51
+		$this->module = $module_service->findByInterface(CertificatesModule::class)->first();
52
+		$this->certif_filesystem = $certif_filesystem;
53
+		$this->url_obfuscator_service = $url_obfuscator_service;
54
+	}
55 55
 
56
-    /**
57
-     * {@inheritDoc}
58
-     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
59
-     */
60
-    public function handle(ServerRequestInterface $request): ResponseInterface
61
-    {
62
-        if ($this->module === null) {
63
-            throw new HttpNotFoundException(I18N::translate('The attached module could not be found.'));
64
-        }
56
+	/**
57
+	 * {@inheritDoc}
58
+	 * @see \Psr\Http\Server\RequestHandlerInterface::handle()
59
+	 */
60
+	public function handle(ServerRequestInterface $request): ResponseInterface
61
+	{
62
+		if ($this->module === null) {
63
+			throw new HttpNotFoundException(I18N::translate('The attached module could not be found.'));
64
+		}
65 65
 
66
-        $tree = Validator::attributes($request)->tree();
66
+		$tree = Validator::attributes($request)->tree();
67 67
 
68
-        $title = I18N::translate('Certificates');
68
+		$title = I18N::translate('Certificates');
69 69
 
70
-        $cities = array_map(function (string $item): array {
71
-            return [$this->url_obfuscator_service->obfuscate($item), $item];
72
-        }, $this->certif_filesystem->cities($tree));
70
+		$cities = array_map(function (string $item): array {
71
+			return [$this->url_obfuscator_service->obfuscate($item), $item];
72
+		}, $this->certif_filesystem->cities($tree));
73 73
 
74
-        $city = Validator::queryParams($request)->string(
75
-            'cityobf',
76
-            Validator::attributes($request)->string('cityobf', '')
77
-        );
74
+		$city = Validator::queryParams($request)->string(
75
+			'cityobf',
76
+			Validator::attributes($request)->string('cityobf', '')
77
+		);
78 78
 
79
-        if ($city !== '' && $this->url_obfuscator_service->tryDeobfuscate($city)) {
80
-            $title = I18N::translate('Certificates for %s', $city);
81
-            $certificates = $this->certif_filesystem->certificatesForCity($tree, $city);
82
-        }
79
+		if ($city !== '' && $this->url_obfuscator_service->tryDeobfuscate($city)) {
80
+			$title = I18N::translate('Certificates for %s', $city);
81
+			$certificates = $this->certif_filesystem->certificatesForCity($tree, $city);
82
+		}
83 83
 
84
-        return $this->viewResponse($this->module->name() . '::certificates-list', [
85
-            'title'                     =>  $title,
86
-            'tree'                      =>  $tree,
87
-            'module_name'               =>  $this->module->name(),
88
-            'cities'                    =>  $cities,
89
-            'selected_city'             =>  $city,
90
-            'certificates_list'         =>  $certificates ?? collect(),
91
-            'url_obfuscator_service'    =>  $this->url_obfuscator_service
92
-        ]);
93
-    }
84
+		return $this->viewResponse($this->module->name() . '::certificates-list', [
85
+			'title'                     =>  $title,
86
+			'tree'                      =>  $tree,
87
+			'module_name'               =>  $this->module->name(),
88
+			'cities'                    =>  $cities,
89
+			'selected_city'             =>  $city,
90
+			'certificates_list'         =>  $certificates ?? collect(),
91
+			'url_obfuscator_service'    =>  $this->url_obfuscator_service
92
+		]);
93
+	}
94 94
 }
Please login to merge, or discard this patch.
app/Module/Certificates/Services/CertificateDataService.php 1 patch
Indentation   +132 added lines, -132 removed lines patch added patch discarded remove patch
@@ -28,144 +28,144 @@
 block discarded – undo
28 28
  */
29 29
 class CertificateDataService
30 30
 {
31
-    /**
32
-     * Find individuals linked to a certificate.
33
-     *
34
-     * {@internal Ideally, the underscore should be escaped in the WHERE clause,
35
-     * but does not work with Sqlite if no default escape has been defined}
36
-     *
37
-     * @param Certificate $certificate
38
-     * @return Collection<\Fisharebest\Webtrees\Individual>
39
-     */
40
-    public function linkedIndividuals(Certificate $certificate): Collection
41
-    {
42
-        $tree = $certificate->tree();
43
-        return DB::table('individuals')
44
-            ->where('i_file', '=', $tree->id())
45
-            ->where('i_gedcom', 'like', '% _ACT ' . $this->escapedSqlPath($certificate) . '%')
46
-            ->select(['individuals.*'])
47
-            ->get()
48
-            ->map(Registry::individualFactory()->mapper($tree))
49
-            ->filter(GedcomRecord::accessFilter());
50
-    }
31
+	/**
32
+	 * Find individuals linked to a certificate.
33
+	 *
34
+	 * {@internal Ideally, the underscore should be escaped in the WHERE clause,
35
+	 * but does not work with Sqlite if no default escape has been defined}
36
+	 *
37
+	 * @param Certificate $certificate
38
+	 * @return Collection<\Fisharebest\Webtrees\Individual>
39
+	 */
40
+	public function linkedIndividuals(Certificate $certificate): Collection
41
+	{
42
+		$tree = $certificate->tree();
43
+		return DB::table('individuals')
44
+			->where('i_file', '=', $tree->id())
45
+			->where('i_gedcom', 'like', '% _ACT ' . $this->escapedSqlPath($certificate) . '%')
46
+			->select(['individuals.*'])
47
+			->get()
48
+			->map(Registry::individualFactory()->mapper($tree))
49
+			->filter(GedcomRecord::accessFilter());
50
+	}
51 51
 
52
-    /**
53
-     * Find families linked to a certificate.
54
-     *
55
-     * {@internal Ideally, the underscore should be escaped in the WHERE clause,
56
-     * but does not work with Sqlite if no default escape has been defined}
57
-     *
58
-     * @param Certificate $certificate
59
-     * @return Collection<\Fisharebest\Webtrees\Family>
60
-     */
61
-    public function linkedFamilies(Certificate $certificate): Collection
62
-    {
63
-        $tree = $certificate->tree();
64
-        return DB::table('families')
65
-            ->where('f_file', '=', $tree->id())
66
-            ->where('f_gedcom', 'like', '% _ACT ' . $this->escapedSqlPath($certificate) . '%')
67
-            ->select(['families.*'])
68
-            ->get()
69
-            ->map(Registry::familyFactory()->mapper($tree))
70
-            ->filter(GedcomRecord::accessFilter());
71
-    }
52
+	/**
53
+	 * Find families linked to a certificate.
54
+	 *
55
+	 * {@internal Ideally, the underscore should be escaped in the WHERE clause,
56
+	 * but does not work with Sqlite if no default escape has been defined}
57
+	 *
58
+	 * @param Certificate $certificate
59
+	 * @return Collection<\Fisharebest\Webtrees\Family>
60
+	 */
61
+	public function linkedFamilies(Certificate $certificate): Collection
62
+	{
63
+		$tree = $certificate->tree();
64
+		return DB::table('families')
65
+			->where('f_file', '=', $tree->id())
66
+			->where('f_gedcom', 'like', '% _ACT ' . $this->escapedSqlPath($certificate) . '%')
67
+			->select(['families.*'])
68
+			->get()
69
+			->map(Registry::familyFactory()->mapper($tree))
70
+			->filter(GedcomRecord::accessFilter());
71
+	}
72 72
 
73
-    /**
74
-     * Find media objects linked to a certificate.
75
-     *
76
-     * {@internal Ideally, the underscore should be escaped in the WHERE clause,
77
-     * but does not work with Sqlite if no default escape has been defined}
78
-     *
79
-     * @param Certificate $certificate
80
-     * @return Collection<\Fisharebest\Webtrees\Media>
81
-     */
82
-    public function linkedMedias(Certificate $certificate): Collection
83
-    {
84
-        $tree = $certificate->tree();
85
-        return DB::table('media')
86
-            ->where('m_file', '=', $tree->id())
87
-            ->where('m_gedcom', 'like', '% _ACT ' . $this->escapedSqlPath($certificate) . '%')
88
-            ->select(['media.*'])
89
-            ->get()
90
-            ->map(Registry::mediaFactory()->mapper($tree))
91
-            ->filter(GedcomRecord::accessFilter());
92
-    }
73
+	/**
74
+	 * Find media objects linked to a certificate.
75
+	 *
76
+	 * {@internal Ideally, the underscore should be escaped in the WHERE clause,
77
+	 * but does not work with Sqlite if no default escape has been defined}
78
+	 *
79
+	 * @param Certificate $certificate
80
+	 * @return Collection<\Fisharebest\Webtrees\Media>
81
+	 */
82
+	public function linkedMedias(Certificate $certificate): Collection
83
+	{
84
+		$tree = $certificate->tree();
85
+		return DB::table('media')
86
+			->where('m_file', '=', $tree->id())
87
+			->where('m_gedcom', 'like', '% _ACT ' . $this->escapedSqlPath($certificate) . '%')
88
+			->select(['media.*'])
89
+			->get()
90
+			->map(Registry::mediaFactory()->mapper($tree))
91
+			->filter(GedcomRecord::accessFilter());
92
+	}
93 93
 
94
-    /**
95
-     * Find notes linked to a certificate.
96
-     *
97
-     * {@internal Ideally, the underscore should be escaped in the WHERE clause,
98
-     * but does not work with Sqlite if no default escape has been defined}
99
-     *
100
-     * @param Certificate $certificate
101
-     * @return Collection<\Fisharebest\Webtrees\Note>
102
-     */
103
-    public function linkedNotes(Certificate $certificate): Collection
104
-    {
105
-        $tree = $certificate->tree();
106
-        return DB::table('other')
107
-            ->where('o_file', '=', $tree->id())
108
-            ->where('o_type', '=', 'NOTE')
109
-            ->where('o_gedcom', 'like', '% _ACT ' . $this->escapedSqlPath($certificate) . '%')
110
-            ->select(['other.*'])
111
-            ->get()
112
-            ->map(Registry::noteFactory()->mapper($tree))
113
-            ->filter(GedcomRecord::accessFilter());
114
-    }
94
+	/**
95
+	 * Find notes linked to a certificate.
96
+	 *
97
+	 * {@internal Ideally, the underscore should be escaped in the WHERE clause,
98
+	 * but does not work with Sqlite if no default escape has been defined}
99
+	 *
100
+	 * @param Certificate $certificate
101
+	 * @return Collection<\Fisharebest\Webtrees\Note>
102
+	 */
103
+	public function linkedNotes(Certificate $certificate): Collection
104
+	{
105
+		$tree = $certificate->tree();
106
+		return DB::table('other')
107
+			->where('o_file', '=', $tree->id())
108
+			->where('o_type', '=', 'NOTE')
109
+			->where('o_gedcom', 'like', '% _ACT ' . $this->escapedSqlPath($certificate) . '%')
110
+			->select(['other.*'])
111
+			->get()
112
+			->map(Registry::noteFactory()->mapper($tree))
113
+			->filter(GedcomRecord::accessFilter());
114
+	}
115 115
 
116
-    /**
117
-     * Return an escaped string to be used in SQL LIKE comparisons.
118
-     * This would not work well for Sqlite, if the escape character is not defined.
119
-     *
120
-     * @param Certificate $certificate
121
-     * @return string
122
-     */
123
-    protected function escapedSqlPath(Certificate $certificate): string
124
-    {
125
-        return str_replace(array('\\', '%', '_'), array('\\\\', '\\%', '\\_'), $certificate->gedcomPath());
126
-    }
116
+	/**
117
+	 * Return an escaped string to be used in SQL LIKE comparisons.
118
+	 * This would not work well for Sqlite, if the escape character is not defined.
119
+	 *
120
+	 * @param Certificate $certificate
121
+	 * @return string
122
+	 */
123
+	protected function escapedSqlPath(Certificate $certificate): string
124
+	{
125
+		return str_replace(array('\\', '%', '_'), array('\\\\', '\\%', '\\_'), $certificate->gedcomPath());
126
+	}
127 127
 
128
-    /**
129
-     * Find a source linked to a citation where a certificate file is referenced.
130
-     *
131
-     * @param Certificate $certificate
132
-     * @return Source|NULL
133
-     */
134
-    public function oneLinkedSource(Certificate $certificate): ?Source
135
-    {
136
-        $regex_query = preg_quote($certificate->gedcomPath(), '/');
137
-        $regex_pattern = '/[1-9] SOUR @(' . Gedcom::REGEX_XREF . ')@(?:\n[2-9]\s(?:(?!SOUR)\w+)\s.*)*\n[2-9] _ACT ' . $regex_query . '/i'; //phpcs:ignore Generic.Files.LineLength.TooLong
128
+	/**
129
+	 * Find a source linked to a citation where a certificate file is referenced.
130
+	 *
131
+	 * @param Certificate $certificate
132
+	 * @return Source|NULL
133
+	 */
134
+	public function oneLinkedSource(Certificate $certificate): ?Source
135
+	{
136
+		$regex_query = preg_quote($certificate->gedcomPath(), '/');
137
+		$regex_pattern = '/[1-9] SOUR @(' . Gedcom::REGEX_XREF . ')@(?:\n[2-9]\s(?:(?!SOUR)\w+)\s.*)*\n[2-9] _ACT ' . $regex_query . '/i'; //phpcs:ignore Generic.Files.LineLength.TooLong
138 138
 
139
-        foreach ($this->linkedRecordsLists($certificate) as $linked_records) {
140
-            foreach ($linked_records as $gedcom_record) {
141
-                foreach ($gedcom_record->facts() as $fact) {
142
-                    if (preg_match_all($regex_pattern, $fact->gedcom(), $matches, PREG_SET_ORDER) >= 1) {
143
-                        foreach ($matches as $match) {
144
-                            $source = Registry::sourceFactory()->make($match[1], $certificate->tree());
145
-                            if ($source !== null && $source->canShowName()) {
146
-                                return $source;
147
-                            }
148
-                        }
149
-                    }
150
-                }
151
-            }
152
-        }
139
+		foreach ($this->linkedRecordsLists($certificate) as $linked_records) {
140
+			foreach ($linked_records as $gedcom_record) {
141
+				foreach ($gedcom_record->facts() as $fact) {
142
+					if (preg_match_all($regex_pattern, $fact->gedcom(), $matches, PREG_SET_ORDER) >= 1) {
143
+						foreach ($matches as $match) {
144
+							$source = Registry::sourceFactory()->make($match[1], $certificate->tree());
145
+							if ($source !== null && $source->canShowName()) {
146
+								return $source;
147
+							}
148
+						}
149
+					}
150
+				}
151
+			}
152
+		}
153 153
 
154
-        return null;
155
-    }
154
+		return null;
155
+	}
156 156
 
157
-    /**
158
-     * Yield lists of gedcom records linked to a certificate.
159
-     *
160
-     * @param Certificate $certificate
161
-     * @return Generator<int, Collection<GedcomRecord>, mixed, void>
162
-     * @psalm-suppress InvalidReturnType
163
-     */
164
-    protected function linkedRecordsLists(Certificate $certificate): Generator
165
-    {
166
-        yield $this->linkedIndividuals($certificate);
167
-        yield $this->linkedFamilies($certificate);
168
-        yield $this->linkedMedias($certificate);
169
-        yield $this->linkedNotes($certificate);
170
-    }
157
+	/**
158
+	 * Yield lists of gedcom records linked to a certificate.
159
+	 *
160
+	 * @param Certificate $certificate
161
+	 * @return Generator<int, Collection<GedcomRecord>, mixed, void>
162
+	 * @psalm-suppress InvalidReturnType
163
+	 */
164
+	protected function linkedRecordsLists(Certificate $certificate): Generator
165
+	{
166
+		yield $this->linkedIndividuals($certificate);
167
+		yield $this->linkedFamilies($certificate);
168
+		yield $this->linkedMedias($certificate);
169
+		yield $this->linkedNotes($certificate);
170
+	}
171 171
 }
Please login to merge, or discard this patch.
app/Module/Certificates/Services/UrlObfuscatorService.php 1 patch
Indentation   +116 added lines, -116 removed lines patch added patch discarded remove patch
@@ -23,120 +23,120 @@
 block discarded – undo
23 23
  */
24 24
 class UrlObfuscatorService
25 25
 {
26
-    /**
27
-     * @var string|null $encryption_key
28
-     */
29
-    private $encryption_key;
30
-
31
-
32
-    /**
33
-     * Return (and generate) the key to be used for the encryption step
34
-     *
35
-     * @return string Encryption key
36
-     */
37
-    protected function encryptionKey(): string
38
-    {
39
-        if ($this->encryption_key === null) {
40
-            /** @var ServerRequestInterface $request **/
41
-            $request = app(ServerRequestInterface::class);
42
-            $server_name = $request->getServerParams()['SERVER_NAME'] ?? '';
43
-            $server_software = $request->getServerParams()['SERVER_SOFTWARE'] ?? '';
44
-            $this->encryption_key = str_pad(md5(
45
-                $server_name !== '' && $server_software !== '' ?
46
-                $server_name . $server_software :
47
-                'STANDARDKEYIFNOSERVER'
48
-            ), SODIUM_CRYPTO_SECRETBOX_KEYBYTES, "1234567890ABCDEF");
49
-        }
50
-        return $this->encryption_key;
51
-    }
52
-
53
-    /**
54
-     * Obfuscate a clear text, with a combination of encryption and base64 encoding.
55
-     * The return string is URL-safe.
56
-     *
57
-     * @param string $cleartext Text to obfuscate
58
-     * @param string $key
59
-     * @param string $nonce
60
-     * @return string
61
-     */
62
-    public function obfuscate(string $cleartext, string $key = '', string $nonce = ''): string
63
-    {
64
-        if ($nonce === '') {
65
-            $nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
66
-        }
67
-        if ($key === '') {
68
-            $key = $this->encryptionKey();
69
-        }
70
-
71
-        if (strlen($nonce) !== SODIUM_CRYPTO_SECRETBOX_NONCEBYTES) {
72
-            throw new InvalidArgumentException('The nonce needs to be SODIUM_CRYPTO_SECRETBOX_NONCEBYTES long');
73
-        }
74
-
75
-        if (strlen($key) !== SODIUM_CRYPTO_SECRETBOX_KEYBYTES) {
76
-            throw new InvalidArgumentException('The key needs to be SODIUM_CRYPTO_SECRETBOX_KEYBYTES long');
77
-        }
78
-
79
-        $encryted = sodium_crypto_secretbox($cleartext, $nonce, $key);
80
-        return strtr(base64_encode($nonce . $encryted), '+/=', '._-');
81
-    }
82
-
83
-    /**
84
-     * Deobfuscate a string from an URL to a clear text.
85
-     *
86
-     * @param string $obfuscated Text to deobfuscate
87
-     * @param string $key
88
-     * @throws InvalidArgumentException
89
-     * @return string
90
-     */
91
-    public function deobfuscate(string $obfuscated, string $key = ''): string
92
-    {
93
-        $obfuscated = strtr($obfuscated, '._-', '+/=');
94
-        if ($key === '') {
95
-            $key = $this->encryptionKey();
96
-        }
97
-
98
-        if (strlen($key) !== SODIUM_CRYPTO_SECRETBOX_KEYBYTES) {
99
-            throw new InvalidArgumentException('The key needs to be SODIUM_CRYPTO_SECRETBOX_KEYBYTES long');
100
-        }
101
-
102
-        $encrypted = base64_decode($obfuscated, true);
103
-        if ($encrypted === false) {
104
-            throw new InvalidArgumentException('The encrypted value is not in correct base64 format.');
105
-        }
106
-
107
-        if (mb_strlen($encrypted, '8bit') < (SODIUM_CRYPTO_SECRETBOX_NONCEBYTES + SODIUM_CRYPTO_SECRETBOX_MACBYTES)) {
108
-            throw new InvalidArgumentException('The encrypted value does not contain enough characters for the key.');
109
-        }
110
-
111
-        $nonce = mb_substr($encrypted, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit');
112
-        $ciphertext = mb_substr($encrypted, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit');
113
-
114
-        $decrypted = sodium_crypto_secretbox_open($ciphertext, $nonce, $key);
115
-
116
-        if ($decrypted === false) {
117
-            throw new InvalidArgumentException('The message has been tampered with in transit.');
118
-        }
119
-
120
-        //sodium_memzero($ciphertext);    // sodium_compat cannot handle it, only through libsodium
121
-
122
-        /** @var string $decrypted - Psalm detect as string|true otherwise */
123
-        return $decrypted;
124
-    }
125
-
126
-    /**
127
-     * Try to deobfuscate a string from an URL to a clear text, returning whether the operation is a success.
128
-     *
129
-     * @param string $obfuscated Text to deobfuscate
130
-     * @param string $key
131
-     * @return bool
132
-     */
133
-    public function tryDeobfuscate(string &$obfuscated, string $key = ''): bool
134
-    {
135
-        try {
136
-            $obfuscated = $this->deobfuscate($obfuscated, $key);
137
-            return true;
138
-        } catch (InvalidArgumentException $ex) {
139
-        }
140
-        return false;
141
-    }
26
+	/**
27
+	 * @var string|null $encryption_key
28
+	 */
29
+	private $encryption_key;
30
+
31
+
32
+	/**
33
+	 * Return (and generate) the key to be used for the encryption step
34
+	 *
35
+	 * @return string Encryption key
36
+	 */
37
+	protected function encryptionKey(): string
38
+	{
39
+		if ($this->encryption_key === null) {
40
+			/** @var ServerRequestInterface $request **/
41
+			$request = app(ServerRequestInterface::class);
42
+			$server_name = $request->getServerParams()['SERVER_NAME'] ?? '';
43
+			$server_software = $request->getServerParams()['SERVER_SOFTWARE'] ?? '';
44
+			$this->encryption_key = str_pad(md5(
45
+				$server_name !== '' && $server_software !== '' ?
46
+				$server_name . $server_software :
47
+				'STANDARDKEYIFNOSERVER'
48
+			), SODIUM_CRYPTO_SECRETBOX_KEYBYTES, "1234567890ABCDEF");
49
+		}
50
+		return $this->encryption_key;
51
+	}
52
+
53
+	/**
54
+	 * Obfuscate a clear text, with a combination of encryption and base64 encoding.
55
+	 * The return string is URL-safe.
56
+	 *
57
+	 * @param string $cleartext Text to obfuscate
58
+	 * @param string $key
59
+	 * @param string $nonce
60
+	 * @return string
61
+	 */
62
+	public function obfuscate(string $cleartext, string $key = '', string $nonce = ''): string
63
+	{
64
+		if ($nonce === '') {
65
+			$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
66
+		}
67
+		if ($key === '') {
68
+			$key = $this->encryptionKey();
69
+		}
70
+
71
+		if (strlen($nonce) !== SODIUM_CRYPTO_SECRETBOX_NONCEBYTES) {
72
+			throw new InvalidArgumentException('The nonce needs to be SODIUM_CRYPTO_SECRETBOX_NONCEBYTES long');
73
+		}
74
+
75
+		if (strlen($key) !== SODIUM_CRYPTO_SECRETBOX_KEYBYTES) {
76
+			throw new InvalidArgumentException('The key needs to be SODIUM_CRYPTO_SECRETBOX_KEYBYTES long');
77
+		}
78
+
79
+		$encryted = sodium_crypto_secretbox($cleartext, $nonce, $key);
80
+		return strtr(base64_encode($nonce . $encryted), '+/=', '._-');
81
+	}
82
+
83
+	/**
84
+	 * Deobfuscate a string from an URL to a clear text.
85
+	 *
86
+	 * @param string $obfuscated Text to deobfuscate
87
+	 * @param string $key
88
+	 * @throws InvalidArgumentException
89
+	 * @return string
90
+	 */
91
+	public function deobfuscate(string $obfuscated, string $key = ''): string
92
+	{
93
+		$obfuscated = strtr($obfuscated, '._-', '+/=');
94
+		if ($key === '') {
95
+			$key = $this->encryptionKey();
96
+		}
97
+
98
+		if (strlen($key) !== SODIUM_CRYPTO_SECRETBOX_KEYBYTES) {
99
+			throw new InvalidArgumentException('The key needs to be SODIUM_CRYPTO_SECRETBOX_KEYBYTES long');
100
+		}
101
+
102
+		$encrypted = base64_decode($obfuscated, true);
103
+		if ($encrypted === false) {
104
+			throw new InvalidArgumentException('The encrypted value is not in correct base64 format.');
105
+		}
106
+
107
+		if (mb_strlen($encrypted, '8bit') < (SODIUM_CRYPTO_SECRETBOX_NONCEBYTES + SODIUM_CRYPTO_SECRETBOX_MACBYTES)) {
108
+			throw new InvalidArgumentException('The encrypted value does not contain enough characters for the key.');
109
+		}
110
+
111
+		$nonce = mb_substr($encrypted, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit');
112
+		$ciphertext = mb_substr($encrypted, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit');
113
+
114
+		$decrypted = sodium_crypto_secretbox_open($ciphertext, $nonce, $key);
115
+
116
+		if ($decrypted === false) {
117
+			throw new InvalidArgumentException('The message has been tampered with in transit.');
118
+		}
119
+
120
+		//sodium_memzero($ciphertext);    // sodium_compat cannot handle it, only through libsodium
121
+
122
+		/** @var string $decrypted - Psalm detect as string|true otherwise */
123
+		return $decrypted;
124
+	}
125
+
126
+	/**
127
+	 * Try to deobfuscate a string from an URL to a clear text, returning whether the operation is a success.
128
+	 *
129
+	 * @param string $obfuscated Text to deobfuscate
130
+	 * @param string $key
131
+	 * @return bool
132
+	 */
133
+	public function tryDeobfuscate(string &$obfuscated, string $key = ''): bool
134
+	{
135
+		try {
136
+			$obfuscated = $this->deobfuscate($obfuscated, $key);
137
+			return true;
138
+		} catch (InvalidArgumentException $ex) {
139
+		}
140
+		return false;
141
+	}
142 142
 }
Please login to merge, or discard this patch.
app/Module/Certificates/Model/Certificate.php 1 patch
Indentation   +189 added lines, -189 removed lines patch added patch discarded remove patch
@@ -25,193 +25,193 @@
 block discarded – undo
25 25
  */
26 26
 class Certificate
27 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
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) {
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
-    }
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
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) {
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 217
 }
Please login to merge, or discard this patch.
app/Module/IsSourced/Data/FactSourceStatus.php 1 patch
Indentation   +229 added lines, -229 removed lines patch added patch discarded remove patch
@@ -22,233 +22,233 @@
 block discarded – undo
22 22
  */
23 23
 class FactSourceStatus extends SourceStatus
24 24
 {
25
-    /**
26
-     * @var boolean $has_date
27
-     */
28
-    private $has_date = false;
29
-
30
-    /**
31
-     * @var boolean $has_precise_date
32
-     */
33
-    private $has_precise_date = false;
34
-
35
-    /**
36
-     * @var boolean $has_source_date
37
-     */
38
-    private $has_source_date = false;
39
-
40
-    /**
41
-     * @var boolean $source_date_match
42
-     */
43
-    private $source_date_match = false;
44
-
45
-    /**
46
-     * Return whether the fact is dated.
47
-     *
48
-     * @return bool
49
-     */
50
-    public function factHasDate(): bool
51
-    {
52
-        return $this->has_date;
53
-    }
54
-
55
-    /**
56
-     * Set whether the fact is dated.
57
-     *
58
-     * @param bool $has_date
59
-     * @return $this
60
-     */
61
-    public function setFactHasDate(bool $has_date): self
62
-    {
63
-        $this->has_date = $has_date;
64
-        return $this;
65
-    }
66
-
67
-    /**
68
-     * Combinate whether the fact is dated with the previous status.
69
-     *
70
-     * @param bool $has_date
71
-     * @return $this
72
-     */
73
-    public function addFactHasDate(bool $has_date): self
74
-    {
75
-        $this->has_date = $this->has_date || $has_date;
76
-        return $this;
77
-    }
78
-
79
-    /**
80
-     * Return whether the fact is dated with a precise day.
81
-     * Any date modifier will be considered as not precise.
82
-     * A month or year will be considered as not precise.
83
-     *
84
-     * @return bool
85
-     */
86
-    public function factHasPreciseDate(): bool
87
-    {
88
-        return $this->has_date && $this->has_precise_date;
89
-    }
90
-
91
-    /**
92
-     * Set whather the fact is dated with a precise day.
93
-     *
94
-     * @param bool $has_precise_date
95
-     * @return $this
96
-     */
97
-    public function setFactHasPreciseDate(bool $has_precise_date): self
98
-    {
99
-        $this->has_precise_date = $has_precise_date;
100
-        return $this;
101
-    }
102
-
103
-    /**
104
-     * Combine whether the fact is dated with a precise day.
105
-     *
106
-     * @param bool $has_precise_date
107
-     * @return $this
108
-     */
109
-    public function addFactHasPreciseDate(bool $has_precise_date): self
110
-    {
111
-        $this->has_precise_date = $this->has_precise_date || $has_precise_date;
112
-        return $this;
113
-    }
114
-
115
-    /**
116
-     * Return whether the source citation is dated.
117
-     *
118
-     * @return bool
119
-     */
120
-    public function sourceHasDate(): bool
121
-    {
122
-        return $this->has_source_date;
123
-    }
124
-
125
-    /**
126
-     * Set whether the source citation is dated.
127
-     *
128
-     * @param bool $has_source_date
129
-     * @return $this
130
-     */
131
-    public function setSourceHasDate(bool $has_source_date): self
132
-    {
133
-        $this->has_source_date = $has_source_date;
134
-        return $this;
135
-    }
136
-
137
-    /**
138
-     * Combine whether the source citation is dated with the previous status.
139
-     *
140
-     * @param bool $has_source_date
141
-     * @return $this
142
-     */
143
-    public function addSourceHasDate(bool $has_source_date): self
144
-    {
145
-        $this->has_source_date = $this->has_source_date || $has_source_date;
146
-        return $this;
147
-    }
148
-
149
-    /**
150
-     * Return whether the source citation date is close to the fact date.
151
-     *
152
-     * @return bool
153
-     */
154
-    public function sourceMatchesFactDate(): bool
155
-    {
156
-        return $this->has_precise_date && $this->has_source_date && $this->source_date_match;
157
-    }
158
-
159
-    /**
160
-     * Set whether the source citation date is close to the fact date.
161
-     *
162
-     * @param bool $source_date_match
163
-     * @return $this
164
-     */
165
-    public function setSourceMatchesFactDate(bool $source_date_match): self
166
-    {
167
-        $this->source_date_match = $source_date_match;
168
-        return $this;
169
-    }
170
-
171
-    /**
172
-     * Combine whether the source citation date is close to the fact date with the previous status.
173
-     *
174
-     * @param bool $source_date_match
175
-     * @return $this
176
-     */
177
-    public function addSourceMatchesFactDate(bool $source_date_match): self
178
-    {
179
-        $this->source_date_match = $this->source_date_match || $source_date_match;
180
-        return $this;
181
-    }
182
-
183
-    /**
184
-     * {@inheritDoc}
185
-     * @see \MyArtJaub\Webtrees\Module\IsSourced\Data\SourceStatus::isFullySourced()
186
-     */
187
-    public function isFullySourced(): bool
188
-    {
189
-        return parent::isFullySourced() && $this->sourceMatchesFactDate();
190
-    }
191
-
192
-    /**
193
-     * {@inheritDoc}
194
-     * @see \MyArtJaub\Webtrees\Module\IsSourced\Data\SourceStatus::label()
195
-     */
196
-    public function label(string $context): string
197
-    {
198
-        $context_label = Registry::elementFactory()->make($context)->label();
199
-
200
-        if ($this->factHasPreciseDate()) {
201
-            if ($this->hasSource()) {
202
-                if ($this->hasSupportingDocument()) {
203
-                    if ($this->sourceMatchesFactDate()) {
204
-                        return I18N::translate('%s sourced with exact certificate', $context_label);
205
-                    } else {
206
-                        return I18N::translate('%s sourced with a certificate', $context_label);
207
-                    }
208
-                }
209
-
210
-                if ($this->sourceMatchesFactDate()) {
211
-                    return I18N::translate('%s precisely sourced', $context_label);
212
-                }
213
-                return I18N::translate('%s sourced', $context_label);
214
-            }
215
-            return I18N::translate('%s not sourced', $context_label);
216
-        }
217
-
218
-        if ($this->factHasDate()) {
219
-            return I18N::translate('%s not precise', $context_label);
220
-        }
221
-        return I18N::translate('%s not found', $context_label);
222
-    }
223
-
224
-    /**
225
-     * {@inheritDoc}
226
-     * @see \MyArtJaub\Webtrees\Module\IsSourced\Data\SourceStatus::order()
227
-     */
228
-    public function order(): int
229
-    {
230
-        return ($this->factHasDate() ? 1 : 0) * ($this->hasSource() ? 1 : -1) *
231
-            ( 1 + ($this->factHasPreciseDate() ? 1 : 0) * (1 +
232
-                0b010 * ($this->sourceMatchesFactDate() ? 1 : 0) +
233
-                0b100 * ($this->hasSupportingDocument() ? 1 : 0)
234
-            )
235
-        );
236
-    }
237
-
238
-    /**
239
-     * {@inheritDoc}
240
-     * @see \MyArtJaub\Webtrees\Module\IsSourced\Data\SourceStatus::combineWith()
241
-     */
242
-    public function combineWith(SourceStatus $other)
243
-    {
244
-        if ($other instanceof FactSourceStatus) {
245
-            $this->addFactHasDate($other->factHasDate());
246
-            $this->addFactHasPreciseDate($other->factHasPreciseDate());
247
-            $this->addSourceHasDate($other->sourceHasDate());
248
-            $this->addSourceMatchesFactDate($other->sourceMatchesFactDate());
249
-        }
250
-
251
-        parent::combineWith($other);
252
-        return $this;
253
-    }
25
+	/**
26
+	 * @var boolean $has_date
27
+	 */
28
+	private $has_date = false;
29
+
30
+	/**
31
+	 * @var boolean $has_precise_date
32
+	 */
33
+	private $has_precise_date = false;
34
+
35
+	/**
36
+	 * @var boolean $has_source_date
37
+	 */
38
+	private $has_source_date = false;
39
+
40
+	/**
41
+	 * @var boolean $source_date_match
42
+	 */
43
+	private $source_date_match = false;
44
+
45
+	/**
46
+	 * Return whether the fact is dated.
47
+	 *
48
+	 * @return bool
49
+	 */
50
+	public function factHasDate(): bool
51
+	{
52
+		return $this->has_date;
53
+	}
54
+
55
+	/**
56
+	 * Set whether the fact is dated.
57
+	 *
58
+	 * @param bool $has_date
59
+	 * @return $this
60
+	 */
61
+	public function setFactHasDate(bool $has_date): self
62
+	{
63
+		$this->has_date = $has_date;
64
+		return $this;
65
+	}
66
+
67
+	/**
68
+	 * Combinate whether the fact is dated with the previous status.
69
+	 *
70
+	 * @param bool $has_date
71
+	 * @return $this
72
+	 */
73
+	public function addFactHasDate(bool $has_date): self
74
+	{
75
+		$this->has_date = $this->has_date || $has_date;
76
+		return $this;
77
+	}
78
+
79
+	/**
80
+	 * Return whether the fact is dated with a precise day.
81
+	 * Any date modifier will be considered as not precise.
82
+	 * A month or year will be considered as not precise.
83
+	 *
84
+	 * @return bool
85
+	 */
86
+	public function factHasPreciseDate(): bool
87
+	{
88
+		return $this->has_date && $this->has_precise_date;
89
+	}
90
+
91
+	/**
92
+	 * Set whather the fact is dated with a precise day.
93
+	 *
94
+	 * @param bool $has_precise_date
95
+	 * @return $this
96
+	 */
97
+	public function setFactHasPreciseDate(bool $has_precise_date): self
98
+	{
99
+		$this->has_precise_date = $has_precise_date;
100
+		return $this;
101
+	}
102
+
103
+	/**
104
+	 * Combine whether the fact is dated with a precise day.
105
+	 *
106
+	 * @param bool $has_precise_date
107
+	 * @return $this
108
+	 */
109
+	public function addFactHasPreciseDate(bool $has_precise_date): self
110
+	{
111
+		$this->has_precise_date = $this->has_precise_date || $has_precise_date;
112
+		return $this;
113
+	}
114
+
115
+	/**
116
+	 * Return whether the source citation is dated.
117
+	 *
118
+	 * @return bool
119
+	 */
120
+	public function sourceHasDate(): bool
121
+	{
122
+		return $this->has_source_date;
123
+	}
124
+
125
+	/**
126
+	 * Set whether the source citation is dated.
127
+	 *
128
+	 * @param bool $has_source_date
129
+	 * @return $this
130
+	 */
131
+	public function setSourceHasDate(bool $has_source_date): self
132
+	{
133
+		$this->has_source_date = $has_source_date;
134
+		return $this;
135
+	}
136
+
137
+	/**
138
+	 * Combine whether the source citation is dated with the previous status.
139
+	 *
140
+	 * @param bool $has_source_date
141
+	 * @return $this
142
+	 */
143
+	public function addSourceHasDate(bool $has_source_date): self
144
+	{
145
+		$this->has_source_date = $this->has_source_date || $has_source_date;
146
+		return $this;
147
+	}
148
+
149
+	/**
150
+	 * Return whether the source citation date is close to the fact date.
151
+	 *
152
+	 * @return bool
153
+	 */
154
+	public function sourceMatchesFactDate(): bool
155
+	{
156
+		return $this->has_precise_date && $this->has_source_date && $this->source_date_match;
157
+	}
158
+
159
+	/**
160
+	 * Set whether the source citation date is close to the fact date.
161
+	 *
162
+	 * @param bool $source_date_match
163
+	 * @return $this
164
+	 */
165
+	public function setSourceMatchesFactDate(bool $source_date_match): self
166
+	{
167
+		$this->source_date_match = $source_date_match;
168
+		return $this;
169
+	}
170
+
171
+	/**
172
+	 * Combine whether the source citation date is close to the fact date with the previous status.
173
+	 *
174
+	 * @param bool $source_date_match
175
+	 * @return $this
176
+	 */
177
+	public function addSourceMatchesFactDate(bool $source_date_match): self
178
+	{
179
+		$this->source_date_match = $this->source_date_match || $source_date_match;
180
+		return $this;
181
+	}
182
+
183
+	/**
184
+	 * {@inheritDoc}
185
+	 * @see \MyArtJaub\Webtrees\Module\IsSourced\Data\SourceStatus::isFullySourced()
186
+	 */
187
+	public function isFullySourced(): bool
188
+	{
189
+		return parent::isFullySourced() && $this->sourceMatchesFactDate();
190
+	}
191
+
192
+	/**
193
+	 * {@inheritDoc}
194
+	 * @see \MyArtJaub\Webtrees\Module\IsSourced\Data\SourceStatus::label()
195
+	 */
196
+	public function label(string $context): string
197
+	{
198
+		$context_label = Registry::elementFactory()->make($context)->label();
199
+
200
+		if ($this->factHasPreciseDate()) {
201
+			if ($this->hasSource()) {
202
+				if ($this->hasSupportingDocument()) {
203
+					if ($this->sourceMatchesFactDate()) {
204
+						return I18N::translate('%s sourced with exact certificate', $context_label);
205
+					} else {
206
+						return I18N::translate('%s sourced with a certificate', $context_label);
207
+					}
208
+				}
209
+
210
+				if ($this->sourceMatchesFactDate()) {
211
+					return I18N::translate('%s precisely sourced', $context_label);
212
+				}
213
+				return I18N::translate('%s sourced', $context_label);
214
+			}
215
+			return I18N::translate('%s not sourced', $context_label);
216
+		}
217
+
218
+		if ($this->factHasDate()) {
219
+			return I18N::translate('%s not precise', $context_label);
220
+		}
221
+		return I18N::translate('%s not found', $context_label);
222
+	}
223
+
224
+	/**
225
+	 * {@inheritDoc}
226
+	 * @see \MyArtJaub\Webtrees\Module\IsSourced\Data\SourceStatus::order()
227
+	 */
228
+	public function order(): int
229
+	{
230
+		return ($this->factHasDate() ? 1 : 0) * ($this->hasSource() ? 1 : -1) *
231
+			( 1 + ($this->factHasPreciseDate() ? 1 : 0) * (1 +
232
+				0b010 * ($this->sourceMatchesFactDate() ? 1 : 0) +
233
+				0b100 * ($this->hasSupportingDocument() ? 1 : 0)
234
+			)
235
+		);
236
+	}
237
+
238
+	/**
239
+	 * {@inheritDoc}
240
+	 * @see \MyArtJaub\Webtrees\Module\IsSourced\Data\SourceStatus::combineWith()
241
+	 */
242
+	public function combineWith(SourceStatus $other)
243
+	{
244
+		if ($other instanceof FactSourceStatus) {
245
+			$this->addFactHasDate($other->factHasDate());
246
+			$this->addFactHasPreciseDate($other->factHasPreciseDate());
247
+			$this->addSourceHasDate($other->sourceHasDate());
248
+			$this->addSourceMatchesFactDate($other->sourceMatchesFactDate());
249
+		}
250
+
251
+		parent::combineWith($other);
252
+		return $this;
253
+	}
254 254
 }
Please login to merge, or discard this patch.
app/Module/IsSourced/Services/SourceStatusService.php 1 patch
Indentation   +122 added lines, -122 removed lines patch added patch discarded remove patch
@@ -29,126 +29,126 @@
 block discarded – undo
29 29
  */
30 30
 class SourceStatusService
31 31
 {
32
-    /**
33
-     * Maximum timespan between the date of a source and the date of the event to consider the source precise.
34
-     * Arbitrally set to approximately a year around the event date.
35
-     *
36
-     * @var int DATE_PRECISION_MARGIN
37
-     */
38
-    private const DATE_PRECISION_MARGIN = 180;
39
-
40
-    /**
41
-     * Return the status of source citations for a fact.
42
-     *
43
-     * @param Fact $fact
44
-     * @return FactSourceStatus
45
-     */
46
-    public function sourceStatusForFact(Fact $fact): FactSourceStatus
47
-    {
48
-        $source_status = new FactSourceStatus();
49
-
50
-        $date = $fact->date();
51
-        $source_status
52
-            ->setFactHasDate($date->isOK())
53
-            ->setFactHasPreciseDate($date->qual1 === '' && $date->minimumJulianDay() === $date->maximumJulianDay());
54
-
55
-        foreach ($fact->getCitations() as $citation) {
56
-            $source_status
57
-                ->setHasSource(true)
58
-                ->addHasSupportingDocument(preg_match('/\n3 _ACT (?:.*)/', $citation) === 1);
59
-
60
-            preg_match_all("/\n3 DATA(?:\n[4-9] .*)*\n4 DATE (.*)/", $citation, $date_matches, PREG_SET_ORDER);
61
-            foreach ($date_matches as $date_match) {
62
-                $source_date = new Date($date_match[1]);
63
-                $source_status
64
-                    ->addSourceHasDate($source_date->isOK())
65
-                    ->addSourceMatchesFactDate($date->isOK() && $source_date->isOK()
66
-                        && abs($source_date->julianDay() - $date->julianDay()) < self::DATE_PRECISION_MARGIN);
67
-            }
68
-
69
-            if ($source_status->isFullySourced()) {
70
-                return $source_status;
71
-            }
72
-        }
73
-
74
-        return $source_status;
75
-    }
76
-
77
-    /**
78
-     * Return the status of sources for a Gedcom record.
79
-     *
80
-     * @param GedcomRecord $record
81
-     * @return SourceStatus
82
-     */
83
-    public function sourceStatusForRecord(GedcomRecord $record): SourceStatus
84
-    {
85
-        $source_status = new SourceStatus();
86
-
87
-        foreach ($record->facts(['SOUR']) as $source) {
88
-            $source_status
89
-                ->setHasSource(true)
90
-                ->addHasSupportingDocument($source->attribute('_ACT') !== '');
91
-
92
-            if ($source_status->isFullySourced()) {
93
-                return $source_status;
94
-            }
95
-        }
96
-
97
-        return $source_status;
98
-    }
99
-
100
-    /**
101
-     * Return the status of source citations for a list of fact types associated with a record.
102
-     *
103
-     * @param GedcomRecord $record
104
-     * @param string[] $tags
105
-     * @return FactSourceStatus
106
-     */
107
-    public function sourceStatusForFactsWithTags(GedcomRecord $record, array $tags): FactSourceStatus
108
-    {
109
-        $source_status = new NullFactSourceStatus();
110
-
111
-        foreach ($record->facts($tags) as $fact) {
112
-            $source_status = $source_status->combineWith($this->sourceStatusForFact($fact));
113
-            if ($source_status->isFullySourced()) {
114
-                return $source_status;
115
-            }
116
-        }
117
-
118
-        return $source_status;
119
-    }
120
-
121
-    /**
122
-     * Return the status of source citations for an individual's birth events.
123
-     *
124
-     * @param Individual $individual
125
-     * @return FactSourceStatus
126
-     */
127
-    public function sourceStatusForBirth(Individual $individual): FactSourceStatus
128
-    {
129
-        return $this->sourceStatusForFactsWithTags($individual, Gedcom::BIRTH_EVENTS);
130
-    }
131
-
132
-    /**
133
-     * Return the status of source citations for an individual's death events.
134
-     *
135
-     * @param Individual $individual
136
-     * @return FactSourceStatus
137
-     */
138
-    public function sourceStatusForDeath(Individual $individual): FactSourceStatus
139
-    {
140
-        return $this->sourceStatusForFactsWithTags($individual, Gedcom::DEATH_EVENTS);
141
-    }
142
-
143
-    /**
144
-     * Return the status of source citations for a family's marriage events.
145
-     *
146
-     * @param Family $family
147
-     * @return FactSourceStatus
148
-     */
149
-    public function sourceStatusForMarriage(Family $family): FactSourceStatus
150
-    {
151
-        $marr_events = [...Gedcom::MARRIAGE_EVENTS, 'MARC', 'MARL', 'MARS'];
152
-        return $this->sourceStatusForFactsWithTags($family, $marr_events);
153
-    }
32
+	/**
33
+	 * Maximum timespan between the date of a source and the date of the event to consider the source precise.
34
+	 * Arbitrally set to approximately a year around the event date.
35
+	 *
36
+	 * @var int DATE_PRECISION_MARGIN
37
+	 */
38
+	private const DATE_PRECISION_MARGIN = 180;
39
+
40
+	/**
41
+	 * Return the status of source citations for a fact.
42
+	 *
43
+	 * @param Fact $fact
44
+	 * @return FactSourceStatus
45
+	 */
46
+	public function sourceStatusForFact(Fact $fact): FactSourceStatus
47
+	{
48
+		$source_status = new FactSourceStatus();
49
+
50
+		$date = $fact->date();
51
+		$source_status
52
+			->setFactHasDate($date->isOK())
53
+			->setFactHasPreciseDate($date->qual1 === '' && $date->minimumJulianDay() === $date->maximumJulianDay());
54
+
55
+		foreach ($fact->getCitations() as $citation) {
56
+			$source_status
57
+				->setHasSource(true)
58
+				->addHasSupportingDocument(preg_match('/\n3 _ACT (?:.*)/', $citation) === 1);
59
+
60
+			preg_match_all("/\n3 DATA(?:\n[4-9] .*)*\n4 DATE (.*)/", $citation, $date_matches, PREG_SET_ORDER);
61
+			foreach ($date_matches as $date_match) {
62
+				$source_date = new Date($date_match[1]);
63
+				$source_status
64
+					->addSourceHasDate($source_date->isOK())
65
+					->addSourceMatchesFactDate($date->isOK() && $source_date->isOK()
66
+						&& abs($source_date->julianDay() - $date->julianDay()) < self::DATE_PRECISION_MARGIN);
67
+			}
68
+
69
+			if ($source_status->isFullySourced()) {
70
+				return $source_status;
71
+			}
72
+		}
73
+
74
+		return $source_status;
75
+	}
76
+
77
+	/**
78
+	 * Return the status of sources for a Gedcom record.
79
+	 *
80
+	 * @param GedcomRecord $record
81
+	 * @return SourceStatus
82
+	 */
83
+	public function sourceStatusForRecord(GedcomRecord $record): SourceStatus
84
+	{
85
+		$source_status = new SourceStatus();
86
+
87
+		foreach ($record->facts(['SOUR']) as $source) {
88
+			$source_status
89
+				->setHasSource(true)
90
+				->addHasSupportingDocument($source->attribute('_ACT') !== '');
91
+
92
+			if ($source_status->isFullySourced()) {
93
+				return $source_status;
94
+			}
95
+		}
96
+
97
+		return $source_status;
98
+	}
99
+
100
+	/**
101
+	 * Return the status of source citations for a list of fact types associated with a record.
102
+	 *
103
+	 * @param GedcomRecord $record
104
+	 * @param string[] $tags
105
+	 * @return FactSourceStatus
106
+	 */
107
+	public function sourceStatusForFactsWithTags(GedcomRecord $record, array $tags): FactSourceStatus
108
+	{
109
+		$source_status = new NullFactSourceStatus();
110
+
111
+		foreach ($record->facts($tags) as $fact) {
112
+			$source_status = $source_status->combineWith($this->sourceStatusForFact($fact));
113
+			if ($source_status->isFullySourced()) {
114
+				return $source_status;
115
+			}
116
+		}
117
+
118
+		return $source_status;
119
+	}
120
+
121
+	/**
122
+	 * Return the status of source citations for an individual's birth events.
123
+	 *
124
+	 * @param Individual $individual
125
+	 * @return FactSourceStatus
126
+	 */
127
+	public function sourceStatusForBirth(Individual $individual): FactSourceStatus
128
+	{
129
+		return $this->sourceStatusForFactsWithTags($individual, Gedcom::BIRTH_EVENTS);
130
+	}
131
+
132
+	/**
133
+	 * Return the status of source citations for an individual's death events.
134
+	 *
135
+	 * @param Individual $individual
136
+	 * @return FactSourceStatus
137
+	 */
138
+	public function sourceStatusForDeath(Individual $individual): FactSourceStatus
139
+	{
140
+		return $this->sourceStatusForFactsWithTags($individual, Gedcom::DEATH_EVENTS);
141
+	}
142
+
143
+	/**
144
+	 * Return the status of source citations for a family's marriage events.
145
+	 *
146
+	 * @param Family $family
147
+	 * @return FactSourceStatus
148
+	 */
149
+	public function sourceStatusForMarriage(Family $family): FactSourceStatus
150
+	{
151
+		$marr_events = [...Gedcom::MARRIAGE_EVENTS, 'MARC', 'MARL', 'MARS'];
152
+		return $this->sourceStatusForFactsWithTags($family, $marr_events);
153
+	}
154 154
 }
Please login to merge, or discard this patch.
app/Common/GeoDispersion/GeoAnalysis/GeoAnalysisPlace.php 1 patch
Indentation   +129 added lines, -129 removed lines patch added patch discarded remove patch
@@ -28,133 +28,133 @@
 block discarded – undo
28 28
  */
29 29
 class GeoAnalysisPlace
30 30
 {
31
-    /**
32
-     * The default place name for invalid places
33
-     * @var string INVALID_PLACE
34
-     */
35
-    private const INVALID_PLACE = '##INVALID##';
36
-
37
-    private Place $place;
38
-    private bool $is_excluded;
39
-
40
-    /**
41
-     * Constructor for GeoAnalysisPlace
42
-     *
43
-     * @param Tree $tree Default tree
44
-     * @param Place|null $place Place resulting from the analysis
45
-     * @param int $depth Place hierarchy depth defined by the geographical analysis view
46
-     * @param bool $strict_depth Checks whether places with a lower depth than defined should be flagged as invalid
47
-     */
48
-    public function __construct(Tree $tree, ?Place $place, int $depth, bool $strict_depth = false)
49
-    {
50
-        $this->place = $this->extractPlace($place, $depth, $strict_depth) ?? new Place('', $tree);
51
-        $this->is_excluded = false;
52
-    }
53
-
54
-    /**
55
-     * Process the provided Place to determine its status for further usage
56
-     *
57
-     * @param Place|null $place
58
-     * @param int $depth
59
-     * @param bool $strict_depth
60
-     * @return Place|NULL
61
-     */
62
-    private function extractPlace(?Place $place, int $depth, bool $strict_depth): ?Place
63
-    {
64
-        if ($place === null) {
65
-            return null;
66
-        }
67
-        if (mb_strlen($place->gedcomName()) === 0) {
68
-            return null;
69
-        }
70
-        $parts = $place->lastParts($depth);
71
-        if ($strict_depth && $parts->count() !== $depth) {
72
-            return new Place(self::INVALID_PLACE, $place->tree());
73
-        }
74
-        return new Place($parts->implode(', '), $place->tree());
75
-    }
76
-
77
-    /**
78
-     * Get the GeoAnalysis Place key
79
-     *
80
-     * @return string
81
-     */
82
-    public function key(): string
83
-    {
84
-        return $this->place->gedcomName();
85
-    }
86
-
87
-    /**
88
-     * Get the underlying Place object
89
-     *
90
-     * @return Place
91
-     */
92
-    public function place(): Place
93
-    {
94
-        return $this->place;
95
-    }
96
-
97
-    /**
98
-     * Check if the GeoAnalysis Place is in the Known status
99
-     *
100
-     * @return bool
101
-     */
102
-    public function isKnown(): bool
103
-    {
104
-        return !$this->isUnknown();
105
-    }
106
-
107
-    /**
108
-     * Check if the GeoAnalysis Place is in the Unknown status
109
-     *
110
-     * @return bool
111
-     */
112
-    public function isUnknown(): bool
113
-    {
114
-        return mb_strlen($this->place->gedcomName()) === 0;
115
-    }
116
-
117
-    /**
118
-     * Check if the GeoAnalysis Place is in the Invalid status
119
-     *
120
-     * @return bool
121
-     */
122
-    public function isInvalid(): bool
123
-    {
124
-        return $this->place->gedcomName() === self::INVALID_PLACE;
125
-    }
126
-
127
-    /**
128
-     * Check if the GeoAnalysis Place is in the Excluded status
129
-     *
130
-     * @return bool
131
-     */
132
-    public function isExcluded(): bool
133
-    {
134
-        return $this->isUnknown() || $this->isInvalid() || $this->is_excluded;
135
-    }
136
-
137
-    /**
138
-     * Set the GeoAnalysis Place status to Found, if the parameter is true
139
-     *
140
-     * @param bool $include
141
-     * @return $this
142
-     */
143
-    public function include(bool $include = true): self
144
-    {
145
-        $this->is_excluded = !$include;
146
-        return $this;
147
-    }
148
-
149
-    /**
150
-     * Set the GeoAnalysis Place status to Excluded, if the parameter is true
151
-     *
152
-     * @param bool $exclude
153
-     * @return $this
154
-     */
155
-    public function exclude(bool $exclude = true): self
156
-    {
157
-        $this->is_excluded = $exclude;
158
-        return $this;
159
-    }
31
+	/**
32
+	 * The default place name for invalid places
33
+	 * @var string INVALID_PLACE
34
+	 */
35
+	private const INVALID_PLACE = '##INVALID##';
36
+
37
+	private Place $place;
38
+	private bool $is_excluded;
39
+
40
+	/**
41
+	 * Constructor for GeoAnalysisPlace
42
+	 *
43
+	 * @param Tree $tree Default tree
44
+	 * @param Place|null $place Place resulting from the analysis
45
+	 * @param int $depth Place hierarchy depth defined by the geographical analysis view
46
+	 * @param bool $strict_depth Checks whether places with a lower depth than defined should be flagged as invalid
47
+	 */
48
+	public function __construct(Tree $tree, ?Place $place, int $depth, bool $strict_depth = false)
49
+	{
50
+		$this->place = $this->extractPlace($place, $depth, $strict_depth) ?? new Place('', $tree);
51
+		$this->is_excluded = false;
52
+	}
53
+
54
+	/**
55
+	 * Process the provided Place to determine its status for further usage
56
+	 *
57
+	 * @param Place|null $place
58
+	 * @param int $depth
59
+	 * @param bool $strict_depth
60
+	 * @return Place|NULL
61
+	 */
62
+	private function extractPlace(?Place $place, int $depth, bool $strict_depth): ?Place
63
+	{
64
+		if ($place === null) {
65
+			return null;
66
+		}
67
+		if (mb_strlen($place->gedcomName()) === 0) {
68
+			return null;
69
+		}
70
+		$parts = $place->lastParts($depth);
71
+		if ($strict_depth && $parts->count() !== $depth) {
72
+			return new Place(self::INVALID_PLACE, $place->tree());
73
+		}
74
+		return new Place($parts->implode(', '), $place->tree());
75
+	}
76
+
77
+	/**
78
+	 * Get the GeoAnalysis Place key
79
+	 *
80
+	 * @return string
81
+	 */
82
+	public function key(): string
83
+	{
84
+		return $this->place->gedcomName();
85
+	}
86
+
87
+	/**
88
+	 * Get the underlying Place object
89
+	 *
90
+	 * @return Place
91
+	 */
92
+	public function place(): Place
93
+	{
94
+		return $this->place;
95
+	}
96
+
97
+	/**
98
+	 * Check if the GeoAnalysis Place is in the Known status
99
+	 *
100
+	 * @return bool
101
+	 */
102
+	public function isKnown(): bool
103
+	{
104
+		return !$this->isUnknown();
105
+	}
106
+
107
+	/**
108
+	 * Check if the GeoAnalysis Place is in the Unknown status
109
+	 *
110
+	 * @return bool
111
+	 */
112
+	public function isUnknown(): bool
113
+	{
114
+		return mb_strlen($this->place->gedcomName()) === 0;
115
+	}
116
+
117
+	/**
118
+	 * Check if the GeoAnalysis Place is in the Invalid status
119
+	 *
120
+	 * @return bool
121
+	 */
122
+	public function isInvalid(): bool
123
+	{
124
+		return $this->place->gedcomName() === self::INVALID_PLACE;
125
+	}
126
+
127
+	/**
128
+	 * Check if the GeoAnalysis Place is in the Excluded status
129
+	 *
130
+	 * @return bool
131
+	 */
132
+	public function isExcluded(): bool
133
+	{
134
+		return $this->isUnknown() || $this->isInvalid() || $this->is_excluded;
135
+	}
136
+
137
+	/**
138
+	 * Set the GeoAnalysis Place status to Found, if the parameter is true
139
+	 *
140
+	 * @param bool $include
141
+	 * @return $this
142
+	 */
143
+	public function include(bool $include = true): self
144
+	{
145
+		$this->is_excluded = !$include;
146
+		return $this;
147
+	}
148
+
149
+	/**
150
+	 * Set the GeoAnalysis Place status to Excluded, if the parameter is true
151
+	 *
152
+	 * @param bool $exclude
153
+	 * @return $this
154
+	 */
155
+	public function exclude(bool $exclude = true): self
156
+	{
157
+		$this->is_excluded = $exclude;
158
+		return $this;
159
+	}
160 160
 }
Please login to merge, or discard this patch.
app/Common/GeoDispersion/GeoAnalysis/GeoAnalysisResultItem.php 1 patch
Indentation   +57 added lines, -57 removed lines patch added patch discarded remove patch
@@ -20,67 +20,67 @@
 block discarded – undo
20 20
  */
21 21
 class GeoAnalysisResultItem
22 22
 {
23
-    private GeoAnalysisPlace $place;
24
-    private int $count;
23
+	private GeoAnalysisPlace $place;
24
+	private int $count;
25 25
 
26
-    /**
27
-     * Constructor for GeoAnalysisResultItem
28
-     *
29
-     * @param GeoAnalysisPlace $place
30
-     * @param int $count
31
-     */
32
-    public function __construct(GeoAnalysisPlace $place, int $count = 0)
33
-    {
34
-        $this->place = $place;
35
-        $this->count = $count;
36
-    }
26
+	/**
27
+	 * Constructor for GeoAnalysisResultItem
28
+	 *
29
+	 * @param GeoAnalysisPlace $place
30
+	 * @param int $count
31
+	 */
32
+	public function __construct(GeoAnalysisPlace $place, int $count = 0)
33
+	{
34
+		$this->place = $place;
35
+		$this->count = $count;
36
+	}
37 37
 
38
-    /**
39
-     * Get the item key.
40
-     *
41
-     * @return string
42
-     */
43
-    public function key(): string
44
-    {
45
-        return $this->place->key();
46
-    }
38
+	/**
39
+	 * Get the item key.
40
+	 *
41
+	 * @return string
42
+	 */
43
+	public function key(): string
44
+	{
45
+		return $this->place->key();
46
+	}
47 47
 
48
-    /**
49
-     * Get the referenced GeoAnalysis Place
50
-     *
51
-     * @return GeoAnalysisPlace
52
-     */
53
-    public function place(): GeoAnalysisPlace
54
-    {
55
-        return $this->place;
56
-    }
48
+	/**
49
+	 * Get the referenced GeoAnalysis Place
50
+	 *
51
+	 * @return GeoAnalysisPlace
52
+	 */
53
+	public function place(): GeoAnalysisPlace
54
+	{
55
+		return $this->place;
56
+	}
57 57
 
58
-    /**
59
-     * Get the count of occurrences of the GeoAnalysis Place in the analysis
60
-     *
61
-     * @return int
62
-     */
63
-    public function count(): int
64
-    {
65
-        return $this->count;
66
-    }
58
+	/**
59
+	 * Get the count of occurrences of the GeoAnalysis Place in the analysis
60
+	 *
61
+	 * @return int
62
+	 */
63
+	public function count(): int
64
+	{
65
+		return $this->count;
66
+	}
67 67
 
68
-    /**
69
-     * Increment the count of occurrences of the GeoAnalysis Place in the analysis
70
-     *
71
-     * @return $this
72
-     */
73
-    public function increment(): self
74
-    {
75
-        $this->count++;
76
-        return $this;
77
-    }
68
+	/**
69
+	 * Increment the count of occurrences of the GeoAnalysis Place in the analysis
70
+	 *
71
+	 * @return $this
72
+	 */
73
+	public function increment(): self
74
+	{
75
+		$this->count++;
76
+		return $this;
77
+	}
78 78
 
79
-    /**
80
-     * Clone the item object
81
-     */
82
-    public function __clone()
83
-    {
84
-        $this->place = clone $this->place;
85
-    }
79
+	/**
80
+	 * Clone the item object
81
+	 */
82
+	public function __clone()
83
+	{
84
+		$this->place = clone $this->place;
85
+	}
86 86
 }
Please login to merge, or discard this patch.
app/Http/Middleware/AuthTreePreference.php 1 patch
Indentation   +31 added lines, -31 removed lines patch added patch discarded remove patch
@@ -31,40 +31,40 @@
 block discarded – undo
31 31
  */
32 32
 class AuthTreePreference implements MiddlewareInterface
33 33
 {
34
-    /**
35
-     * {@inheritDoc}
36
-     * @see \Psr\Http\Server\MiddlewareInterface::process()
37
-     */
38
-    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
39
-    {
40
-        $tree = Validator::attributes($request)->tree();
41
-        $route = Validator::attributes($request)->route();
42
-        $user = Validator::attributes($request)->user();
34
+	/**
35
+	 * {@inheritDoc}
36
+	 * @see \Psr\Http\Server\MiddlewareInterface::process()
37
+	 */
38
+	public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
39
+	{
40
+		$tree = Validator::attributes($request)->tree();
41
+		$route = Validator::attributes($request)->route();
42
+		$user = Validator::attributes($request)->user();
43 43
 
44
-        $permission_preference = $route->extras['permission_preference'] ?? '';
45
-        $permission_level = $permission_preference === '' ? '' : $tree->getPreference($permission_preference);
44
+		$permission_preference = $route->extras['permission_preference'] ?? '';
45
+		$permission_level = $permission_preference === '' ? '' : $tree->getPreference($permission_preference);
46 46
 
47
-        // Permissions are configured
48
-        if (is_numeric($permission_level)) {
49
-            // Logged in with the correct role?
50
-            if (Auth::accessLevel($tree, $user) <= (int) $permission_level) {
51
-                    return $handler->handle($request);
52
-            }
47
+		// Permissions are configured
48
+		if (is_numeric($permission_level)) {
49
+			// Logged in with the correct role?
50
+			if (Auth::accessLevel($tree, $user) <= (int) $permission_level) {
51
+					return $handler->handle($request);
52
+			}
53 53
 
54
-            // Logged in, but without the correct role?
55
-            if ($user instanceof User) {
56
-                throw new HttpAccessDeniedException();
57
-            }
58
-        }
54
+			// Logged in, but without the correct role?
55
+			if ($user instanceof User) {
56
+				throw new HttpAccessDeniedException();
57
+			}
58
+		}
59 59
 
60
-        // Permissions no configured, or not logged in
61
-        if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
62
-            throw new HttpAccessDeniedException();
63
-        }
60
+		// Permissions no configured, or not logged in
61
+		if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
62
+			throw new HttpAccessDeniedException();
63
+		}
64 64
 
65
-        return Registry::responseFactory()->redirect(
66
-            LoginPage::class,
67
-            ['tree' => $tree->name(), 'url' => (string) $request->getUri()]
68
-        );
69
-    }
65
+		return Registry::responseFactory()->redirect(
66
+			LoginPage::class,
67
+			['tree' => $tree->name(), 'url' => (string) $request->getUri()]
68
+		);
69
+	}
70 70
 }
Please login to merge, or discard this patch.