Sunlight::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 14
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 9
nc 3
nop 2
1
<?php
2
/**
3
 * Sunlight-php
4
 *
5
 * PHP Version 7.0.1
6
 *
7
 * @category Sunlight_Foundation_API
8
 * @package  ContactMyReps
9
 * @author   edfialk <[email protected]>
10
 * @license  http://opensource.org/licenses/MIT MIT License
11
 * @link     https://github.com/contactmyreps/sunlight-php
12
 */
13
14
namespace ContactMyReps\Sunlight;
15
16
use ContactMyReps\Sunlight\API\OpenStates;
17
18
/**
19
* Sunlight Foundation API wrapper
20
* For more information see http://sunlightfoundation.com/api
21
*
22
* @category Sunlight_Foundation_API
23
* @package  ContactMyReps
24
* @author   edfialk <[email protected]>
25
* @license  http://opensource.org/licenses/MIT MIT License
26
* @link     https://github.com/contactmyreps/sunlight-php
27
*/
28
class Sunlight
29
{
30
31
    private static $key = null;
32
33
    public $options = [
34
    'asnyc' => false,
35
    'format' => 'json'
36
    ];
37
38
    /**
39
     * Create the Sunlight object
40
     *
41
     * @param string $key     Sunlight Foundation API Key
42
     * @param array  $options global config options
43
     */
44
    public function __construct($key = null, $options = [])
45
    {
46
        if ($key === null) {
47
            if (self::$key === null) {
48
                $msg = 'No key provided, and none is globally set. ';
49
                $msg .= 'Use Sunlight::setKey, or instantiate the Sunlight class with a $key parameter.';
50
                throw new APIException($msg);
51
            }
52
        } else {
53
            self::$key = $key;
54
        }
55
56
        $this->options = array_merge($this->options, $options);
57
    }
58
59
    /**
60
     * Set or retrieve option value
61
     *
62
     * @param string or array $key   the option key to set/retrieve, or an array of options to set
63
     * @param object          $value value to store
64
     *
65
     * @return object          if key is string and value is null, return options[key] value
66
     */
67 View Code Duplication
    public function option($key, $value = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
    {
69
        if (is_array($key)) {
70
            $this->options = array_merge($this->options, $key);
71
            return;
72
        }
73
74
        if (null === $value) {
75
            return $this->options[$key] ? $this->options[$key] : null;
76
        }
77
78
        $this->options[$key] = $value;
79
    }
80
81
    /**
82
     * Get an API instance
83
     *
84
     * @param string $api     api class path
85
     * @param array  $options local options for api
86
     *
87
     * @return object          api instance
88
     */
89
    public function get($api, $options)
90
    {
91
        return $api::getInstance(self::$key, $options);
92
    }
93
94
    /**
95
     * Get OpenStates API instance
96
     *
97
     * @param array $options OpenStates api options
98
     *
99
     * @return object          OpenStates api
100
     */
101
    public function openStates($options = [])
102
    {
103
        $options = array_merge($this->options, $options);
104
105
        return $this->get('\\ContactMyReps\\Sunlight\\api\\OpenStates', $options);
106
    }
107
108
    /**
109
     * Get Congress API instance
110
     *
111
     * @param array $options Congress api options
112
     *
113
     * @return object          Congress api
114
     */
115
    public function congress($options = [])
116
    {
117
        $options = array_merge($this->options, $options);
118
119
        return $this->get('\\ContactMyReps\\Sunlight\\api\\Congress', $options);
120
    }
121
}
122