Passed
Pull Request — master (#195)
by
unknown
08:18
created

UnpaywallDataService   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 34
c 1
b 0
f 1
dl 0
loc 64
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDataRequest() 0 10 2
A __construct() 0 3 1
A enrichInformation() 0 21 6
A searchRequest() 0 9 2
1
<?php
2
namespace EWW\Dpf\Services\FeUser;
3
4
use EWW\Dpf\Configuration\ClientConfigurationManager;
5
use TYPO3\CMS\Extbase\Object\ObjectManager;
6
use \Httpful\Request;
7
8
class UnpaywallDataService
9
{
10
    /**
11
     * clientConfigurationManager
12
     *
13
     * @var \EWW\Dpf\Configuration\ClientConfigurationManager
14
     * @inject
15
     */
16
    protected $clientConfigurationManager;
17
18
    protected $apiUrl = 'https://api.unpaywall.org/v2/';
19
20
    public function __construct() {
21
        $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(ObjectManager::class);
22
        $this->clientConfigurationManager = $objectManager->get(ClientConfigurationManager::class);
23
    }
24
25
    public function searchRequest($searchTerm) {
26
        $mail = $this->clientConfigurationManager->getSetting("adminEmail");
27
        $response = Request::get($this->apiUrl . '' . $searchTerm .'?email=' . $mail)
28
            ->send();
29
        if ($response->body->HTTP_status_code == 404) {
30
            return ['entries' => ''];
31
        } else {
32
            $response = $this->enrichInformation($response);
33
            return ['entries' => [$response->body]];
34
        }
35
36
    }
37
38
    public function getDataRequest($id) {
39
        $mail = $this->clientConfigurationManager->getSetting("adminEmail");
40
        $response = Request::get($this->apiUrl . '' . $id .'?email=' . $mail)
41
            ->send();
42
43
        if ($response->body->HTTP_status_code == 404) {
44
            return '';
45
        } else {
46
            $response = $this->enrichInformation($response);
47
            return $response->body;
48
        }
49
    }
50
51
    protected function enrichInformation($response) {
52
        $responseBody = $response->body;
53
54
        $is_oa = $responseBody->is_oa;
55
        $journal_is_in_doaj = $responseBody->journal_is_in_doaj;
56
        $host_type = $responseBody->best_oa_location->host_type;
57
58
        $color = '';
59
        if ($is_oa == 'true' && $journal_is_in_doaj == 'false') {
60
            $color = 'hybrid';
61
        }
62
        if ($is_oa == 'true' && $journal_is_in_doaj == 'true') {
63
            $color = 'gold';
64
        }
65
        if ($host_type == 'repository') {
66
            $color = 'green';
67
        }
68
69
        $response->body->color = $color;
70
71
        return $response;
72
    }
73
74
}