Passed
Pull Request — master (#123)
by Sebastian
04:18
created

Profile   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 34
c 2
b 0
f 0
dl 0
loc 103
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAddress() 0 8 2
A __construct() 0 4 1
A getData() 0 11 2
A getFullName() 0 11 2
A getRaw() 0 6 3
1
<?php
2
3
/**
4
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
5
 *
6
 * This file is part of the Kitodo and TYPO3 projects.
7
 *
8
 * @license GNU General Public License version 3 or later.
9
 * For the full copyright and license information, please read the
10
 * LICENSE.txt file that was distributed with this source code.
11
 */
12
13
namespace Kitodo\Dlf\Api\Viaf;
14
15
use Kitodo\Dlf\Common\Helper;
16
use TYPO3\CMS\Core\Http\RequestFactory;
17
use TYPO3\CMS\Core\Log\LogManager;
18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20
/**
21
 * VIAF API Profile class
22
 *
23
 * @author Beatrycze Volk <[email protected]>
24
 * @package TYPO3
25
 * @subpackage dlf
26
 * @access public
27
 **/
28
class Profile
29
{
30
    /**
31
     * This holds the logger
32
     *
33
     * @var LogManager
34
     * @access protected
35
     */
36
    protected $logger;
37
38
    /**
39
     * This holds the client
40
     *
41
     * @var Client
42
     * @access protected
43
     */
44
    protected $client;
45
46
    /**
47
     * The raw VIAF profile
48
     *
49
     * @var \SimpleXmlElement|false
50
     **/
51
    private $raw = null;
52
53
    /**
54
     * Constructs client instance
55
     *
56
     * @param string $viaf: the VIAF identifier of the profile
57
     *
58
     * @return void
59
     **/
60
    public function __construct($viaf)
61
    {
62
        $this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(static::class);
63
        $this->client = new Client($viaf, GeneralUtility::makeInstance(RequestFactory::class));
64
    }
65
66
    /**
67
     * Get the VIAF profile data
68
     *
69
     * @return array|false
70
     **/
71
    public function getData()
72
    {
73
        $this->getRaw();
74
        if (!empty($this->raw)) {
75
            $data = [];
76
            $data['address'] = $this->getAddress();
77
            $data['fullName'] = $this->getFullName();
78
            return $data;
79
        } else {
80
            $this->logger->warning('No data found for given VIAF URL');
0 ignored issues
show
Bug introduced by
The method warning() does not exist on TYPO3\CMS\Core\Log\LogManager. ( Ignorable by Annotation )

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

80
            $this->logger->/** @scrutinizer ignore-call */ 
81
                           warning('No data found for given VIAF URL');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
            return false;
82
        }
83
    }
84
85
    /**
86
     * Get the address
87
     *
88
     * @return string|false
89
     **/
90
    public function getAddress()
91
    {
92
        $this->getRaw();
93
        if (!empty($this->raw->asXML())) {
94
            return (string) $this->raw->xpath('./ns1:nationalityOfEntity/ns1:data/ns1:text')[0];
95
        } else {
96
            $this->logger->warning('No address found for given VIAF URL');
97
            return false;
98
        }
99
    }
100
101
    /**
102
     * Get the full name
103
     *
104
     * @return string|false
105
     **/
106
    public function getFullName()
107
    {
108
        $this->getRaw();
109
        if (!empty($this->raw->asXML())) {
110
            $rawName = $this->raw->xpath('./ns1:mainHeadings/ns1:data/ns1:text');
111
            $name = (string) $rawName[0];
112
            $name = trim(trim(trim($name), ','), '.');
113
            return $name;
114
        } else {
115
            $this->logger->warning('No name found for given VIAF URL');
116
            return false;
117
        }
118
    }
119
120
    /**
121
     * Get the VIAF raw profile data
122
     *
123
     * @return void
124
     **/
125
    protected function getRaw()
126
    {
127
        $data = $this->client->getData();
128
        if (!isset($this->raw) && $data != false) {
129
            $this->raw = Helper::getXmlFileAsString($data);
130
            $this->raw->registerXPathNamespace('ns1', 'http://viaf.org/viaf/terms#');
131
        }
132
    }
133
}
134