SourceCertificateIconHook::extractPath()   C
last analyzed

Complexity

Conditions 13
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 12
nc 4
nop 1
dl 0
loc 19
rs 6.6166
c 0
b 0
f 0

How to fix   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) 2011-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\Hooks;
16
17
use Fisharebest\Webtrees\Auth;
18
use Fisharebest\Webtrees\Fact;
19
use Fisharebest\Webtrees\Source;
20
use Fisharebest\Webtrees\Tree;
21
use Fisharebest\Webtrees\Module\ModuleInterface;
22
use MyArtJaub\Webtrees\Contracts\Hooks\FactSourceTextExtenderInterface;
23
use MyArtJaub\Webtrees\Module\Certificates\CertificatesModule;
24
use MyArtJaub\Webtrees\Module\Certificates\Elements\SourceCertificate;
25
use MyArtJaub\Webtrees\Module\Certificates\Model\Certificate;
26
use MyArtJaub\Webtrees\Module\Certificates\Services\UrlObfuscatorService;
27
28
/**
29
 * Hook for displaying the certificate link icon next to the source citation title.
30
 */
31
class SourceCertificateIconHook implements FactSourceTextExtenderInterface
32
{
33
    private CertificatesModule $module;
34
    private UrlObfuscatorService $url_obfuscator_service;
35
36
    /**
37
     * Constructor for SourceCertificateIconHook
38
     *
39
     * @param CertificatesModule $module
40
     * @param UrlObfuscatorService $url_obfuscator_service
41
     */
42
    public function __construct(CertificatesModule $module, UrlObfuscatorService $url_obfuscator_service)
43
    {
44
        $this->module = $module;
45
        $this->url_obfuscator_service = $url_obfuscator_service;
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookInterface::module()
51
     */
52
    public function module(): ModuleInterface
53
    {
54
        return $this->module;
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     * @see \MyArtJaub\Webtrees\Contracts\Hooks\FactSourceTextExtenderInterface::factSourcePrepend()
60
     */
61
    public function factSourcePrepend(Tree $tree, $fact): string
62
    {
63
        $permission_level = $tree->getPreference('MAJ_CERTIF_SHOW_CERT');
64
        if (is_numeric($permission_level) && Auth::accessLevel($tree) <= (int) $permission_level) {
65
            $path = $this->extractPath($fact);
66
67
            if ($path !== '') {
68
                $certificate = new Certificate($tree, $path);
69
                return view($this->module->name() . '::components/certificate-icon', [
70
                    'module_name'               =>  $this->module->name(),
71
                    'certificate'               =>  $certificate,
72
                    'url_obfuscator_service'    =>  $this->url_obfuscator_service,
73
                    'js_script_url'             =>  $this->module->assetUrl('js/certificates.min.js')
74
                ]);
75
            }
76
        }
77
        return '';
78
    }
79
80
    /**
81
     * Extract path from the provided fact objet.
82
     *
83
     * @param \Fisharebest\Webtrees\Fact|array<array<\Fisharebest\Webtrees\Contracts\ElementInterface|string>> $fact
84
     * @return string
85
     * @psalm-suppress RedundantConditionGivenDocblockType
86
     */
87
    private function extractPath($fact): string
88
    {
89
        if ($fact instanceof Fact && $fact->target() instanceof Source) {
90
            return $fact->attribute('_ACT');
91
        } elseif (
92
            is_array($fact) && count($fact) === 2
93
            && null !== ($source_elements = $fact[0]) && is_array($source_elements) // @phpstan-ignore-line
94
            && null !== ($source_values = $fact[1]) && is_array($source_values) // @phpstan-ignore-line
95
        ) {
96
            foreach ($source_elements as $key => $element) {
97
                if (
98
                    $element instanceof SourceCertificate
99
                    && isset($source_values[$key]) && is_string($source_values[$key])
100
                ) {
101
                    return $element->canonical($source_values[$key]);
102
                }
103
            }
104
        }
105
        return '';
106
    }
107
108
    /**
109
     * {@inheritDoc}
110
     * @see \MyArtJaub\Webtrees\Contracts\Hooks\FactSourceTextExtenderInterface::factSourceAppend()
111
     */
112
    public function factSourceAppend(Tree $tree, $fact): string
113
    {
114
        return '';
115
    }
116
}
117