Passed
Branch master (63074b)
by Dispositif
05:34 queued 02:37
created

OpenAccessTrait::isOpenFromDCrights()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 10
1
<?php
2
/*
3
 * This file is part of dispositif/wikibot application (@github)
4
 * 2019-2023 © Philippe M./Irønie  <[email protected]>
5
 * For the full copyright and MIT license information, view the license file.
6
 */
7
8
declare(strict_types=1);
9
10
11
namespace App\Domain\Publisher\Traits;
12
13
/**
14
 * Open Access of web documents. See https://en.wikipedia.org/wiki/Open_access
15
 * On frwiki mapping "accès url" : libre, inscription, limité, payant/abonnement.
16
 * https://fr.wikipedia.org/wiki/Wikip%C3%A9dia:Acc%C3%A8s_url
17
 */
18
trait OpenAccessTrait
19
{
20
    /**
21
     * no consistency on the mysterious values of DC.rights.
22
     * @var string[]
23
     */
24
    protected $DCopenValues = ['free', 'public domain', 'public', 'domaine public', 'available'];
25
26
    protected function convertURLaccess($data): ?string
27
    {
28
        // https://developers.facebook.com/docs/instant-articles/subscriptions/content-tiering/?locale=fr_FR
29
        if (isset($data['og:article:content_tier'])) {
30
            switch (strtolower($data['og:article:content_tier'])) {
31
                case 'free':
32
                    return 'libre';
33
                case 'locked':
34
                    return 'payant';
35
                case 'metered':
36
                    return 'limité';
37
            }
38
        }
39
40
        // NYT, Figaro
41
        if (isset($data['isAccessibleForFree'])) {
42
            return ($this->sameAsTrue($data['isAccessibleForFree'])) ? 'libre' : 'payant';
43
        }
44
45
        if ($this->isOpenFromDCrights($data) || $this->isOpenFromDCaccessRights($data)) {
46
            return 'libre';
47
        }
48
49
        return null;
50
    }
51
52
    protected function sameAsTrue($str = null): bool
53
    {
54
        if ($str === null) {
55
            return false;
56
        }
57
        if (is_bool($str)) {
58
            return $str;
59
        }
60
        $str = strtolower($str);
61
        return in_array($str, ['true', '1', 'yes', 'oui', 'ok']);
62
    }
63
64
    /**
65
     * TODO : https://terms.tdwg.org/wiki/dcterms:accessRights
66
     * Information about who access the resource or an indication of its security status."
67
     * Values are a mystery...
68
     */
69
    protected function isOpenFromDCrights(array $data): bool
70
    {
71
        if (
72
            isset($data['DC.rights'])
73
            && in_array(strtolower($data['DC.rights']), $this->DCopenValues)
74
        ) {
75
            return true;
76
        }
77
78
        return false;
79
    }
80
81
    /**
82
     * TODO : https://terms.tdwg.org/wiki/dcterms:accessRights
83
     * Information about who access the resource or an indication of its security status."
84
     * Values are a mystery...
85
     */
86
    protected function isOpenFromDCaccessRights(array $data): bool
87
    {
88
        if (
89
            isset($data['DC.accessRights'])
90
            && in_array(strtolower($data['DC.accessRights']), $this->DCopenValues)
91
        ) {
92
            return true;
93
        }
94
95
        return false;
96
    }
97
}