Completed
Push — master ( e530dd...3592ed )
by Guillaume
05:58
created

UrlProvider::getUrlByName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the hogosha-monitor package
5
 *
6
 * Copyright (c) 2016 Guillaume Cavana
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * Feel free to edit as you please, and have fun.
12
 *
13
 * @author Guillaume Cavana <[email protected]>
14
 */
15
16
namespace Hogosha\Monitor\Model;
17
18
use Hogosha\Monitor\Configuration\ConfigurationLoader;
19
use Symfony\Component\OptionsResolver\OptionsResolver;
20
21
/**
22
 * @author Guillaume Cavana <[email protected]>
23
 */
24
class UrlProvider
25
{
26
    /**
27
     * $urls.
28
     *
29
     * @var array
30
     */
31
    protected $urls = [];
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param ConfigurationLoader $configurationLoader
37
     */
38
    public function __construct(ConfigurationLoader $configurationLoader)
39
    {
40
        $resolver = new OptionsResolver();
41
        $resolver->setDefault('metric_uuid', null);
42
        $resolver->setRequired(
43
            [
44
                'url',
45
                'method',
46
                'headers',
47
                'timeout',
48
                'status_code',
49
            ]
50
        );
51
52
        $config = $configurationLoader->loadConfiguration();
53
        foreach ($config['urls'] as $name => $attribute) {
54
            $attribute = $resolver->resolve($attribute);
55
            $this->urls[$name] = new UrlInfo(
56
                $name,
57
                $attribute['url'],
58
                $attribute['method'],
59
                $attribute['headers'],
60
                $attribute['timeout'],
61
                $attribute['status_code'],
62
                $attribute['metric_uuid']
63
            );
64
        }
65
    }
66
67
    /**
68
     * getUrls.
69
     *
70
     * @return array
71
     */
72
    public function getUrls()
73
    {
74
        return $this->urls;
75
    }
76
77
    /**
78
     * getUrlByName.
79
     *
80
     * @param string $url
81
     *
82
     * @return UrlInfo
83
     */
84
    public function getUrlByName($url)
85
    {
86
        if (array_key_exists($url, $this->urls)) {
87
            return $this->urls[$url];
88
        }
89
    }
90
}
91