Completed
Push — master ( 7c429e...75748b )
by Michael
03:23
created

NetworkWiring   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 84
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B wire() 0 75 6
1
<?php
2
/**
3
 * Contains class NetworkWiring.
4
 *
5
 * PHP version 5.5
6
 *
7
 * LICENSE:
8
 * This file is part of Yet Another Php Eve Api Library also know as Yapeal
9
 * which can be used to access the Eve Online API data and place it into a
10
 * database.
11
 * Copyright (C) 2016 Michael Cummings
12
 *
13
 * This program is free software: you can redistribute it and/or modify it
14
 * under the terms of the GNU Lesser General Public License as published by the
15
 * Free Software Foundation, either version 3 of the License, or (at your
16
 * option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful, but WITHOUT
19
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
21
 * for more details.
22
 *
23
 * You should have received a copy of the GNU Lesser General Public License
24
 * along with this program. If not, see
25
 * <http://www.gnu.org/licenses/>.
26
 *
27
 * You should be able to find a copy of this license in the LICENSE.md file. A
28
 * copy of the GNU GPL should also be available in the GNU-GPL.md file.
29
 *
30
 * @copyright 2016 Michael Cummings
31
 * @license   LGPL-3.0+
32
 * @author    Michael Cummings <[email protected]>
33
 */
34
namespace Yapeal\Configuration;
35
36
use Yapeal\Container\ContainerInterface;
37
use Yapeal\Network\GuzzleNetworkRetriever;
38
39
/**
40
 * Class NetworkWiring.
41
 */
42
class NetworkWiring implements WiringInterface
43
{
44
    /**
45
     * @param ContainerInterface $dic
46
     *
47
     * @return self Fluent interface.
48
     * @throws \LogicException
49
     */
50
    public function wire(ContainerInterface $dic)
51
    {
52
        if (empty($dic['Yapeal.Network.Client'])) {
53
            $dic['Yapeal.Network.Client'] = function ($dic) {
54
                $appComment = $dic['Yapeal.Network.appComment'];
55
                $appName = $dic['Yapeal.Network.appName'];
56
                $appVersion = $dic['Yapeal.Network.appVersion'];
57
                if ('' === $appName) {
58
                    $appComment = '';
59
                    $appVersion = '';
60
                }
61
                $userAgent = trim(str_replace([
62
                        '{machineType}',
63
                        '{osName}',
64
                        '{osRelease}',
65
                        '{phpVersion}',
66
                        '{appComment}',
67
                        '{appName}',
68
                        '{appVersion}'
69
                    ], [
70
                        php_uname('m'),
71
                        php_uname('s'),
72
                        php_uname('r'),
73
                        PHP_VERSION,
74
                        $appComment,
75
                        $appName,
76
                        $appVersion
77
                    ], $dic['Yapeal.Network.userAgent'])
78
                );
79
                $userAgent = ltrim($userAgent, '/ ');
80
                $headers = [
81
                    'Accept' => $dic['Yapeal.Network.Headers.Accept'],
82
                    'Accept-Charset' => $dic['Yapeal.Network.Headers.Accept-Charset'],
83
                    'Accept-Encoding' => $dic['Yapeal.Network.Headers.Accept-Encoding'],
84
                    'Accept-Language' => $dic['Yapeal.Network.Headers.Accept-Language'],
85
                    'Connection' => $dic['Yapeal.Network.Headers.Connection'],
86
                    'Keep-Alive' => $dic['Yapeal.Network.Headers.Keep-Alive']
87
                ];
88
                // Clean up any extra spaces and EOL chars from Yaml.
89
                array_walk($headers, function (&$value) {
90
                    /** @noinspection ReferenceMismatchInspection */
91
                    return trim(str_replace(' ', '', $value));
92
                });
93
                if ('' !== $userAgent) {
94
                    $headers['User-Agent'] = $userAgent;
95
                }
96
                $defaults = [
97
                    'base_uri' => $dic['Yapeal.Network.baseUrl'],
98
                    'connect_timeout' => (int)$dic['Yapeal.Network.connect_timeout'],
99
                    'headers' => $headers,
100
                    'timeout' => (int)$dic['Yapeal.Network.timeout'],
101
                    'verify' => $dic['Yapeal.Network.verify']
102
                ];
103
                return new $dic['Yapeal.Network.class']($defaults);
104
            };
105
        }
106
        if (empty($dic['Yapeal.Network.Retriever'])) {
107
            $dic['Yapeal.Network.Retriever'] = function ($dic) {
108
                return new GuzzleNetworkRetriever($dic['Yapeal.Network.Client']);
109
            };
110
        }
111
        if (!isset($dic['Yapeal.Event.Mediator'])) {
112
            $mess = 'Tried to call Mediator before it has been added';
113
            throw new \LogicException($mess);
114
        }
115
        /**
116
         * @var \Yapeal\Event\MediatorInterface $mediator
117
         */
118
        $mediator = $dic['Yapeal.Event.Mediator'];
119
        $mediator->addServiceSubscriberByEventList(
120
            'Yapeal.Network.Retriever',
121
            ['Yapeal.EveApi.retrieve' => ['retrieveEveApi', 'last']]
122
        );
123
        return $this;
124
    }
125
}
126