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

Pusher   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 26 1
A push() 0 17 3
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\Portal;
17
18
use Hogosha\Monitor\Configuration\ConfigurationLoader;
19
use Hogosha\Monitor\Model\ResultCollection;
20
use Hogosha\Monitor\Model\UrlProvider;
21
use Hogosha\Sdk\Api\Client;
22
use Hogosha\Sdk\Resource\Factory\ResourceFactory;
23
use Symfony\Component\OptionsResolver\OptionsResolver;
24
25
/**
26
 * Class Pusher.
27
 */
28
class Pusher
29
{
30
    /**
31
     * $client.
32
     *
33
     * @var Client
34
     */
35
    protected $client;
36
37
    /**
38
     * $urlProvider.
39
     *
40
     * @var UrlProvider
41
     */
42
    protected $urlProvider;
43
44
    /**
45
     * Constructor.
46
     *
47
     * @param UrlProvider         $urlProvider
48
     * @param ConfigurationLoader $configurationLoader
49
     */
50
    public function __construct(
51
        UrlProvider $urlProvider,
52
        ConfigurationLoader $configurationLoader
53
    ) {
54
        $resolver = new OptionsResolver();
55
        $resolver->setRequired(
56
            [
57
                'username',
58
                'password',
59
                'base_uri',
60
            ]
61
        );
62
63
        $this->urlProvider = $urlProvider;
64
        $options = $configurationLoader->loadConfiguration();
65
66
        $options = $resolver->resolve($options['hogosha']);
67
68
        $this->client = new Client(
69
            [
70
                'username' => $options['username'],
71
                'password' => $options['password'],
72
                'base_uri' => $options['base_uri'],
73
            ]
74
        );
75
    }
76
77
    /**
78
     * push.
79
     *
80
     * @param ResultCollection $results
81
     */
82
    public function push(ResultCollection $results)
83
    {
84
        $resourceFactory = new ResourceFactory($this->client);
85
        $metricResource = $resourceFactory->get('metricPoint');
86
87
        foreach ($results as $result) {
88
            try {
89
                $metricResource->createMetricPoint([
90
                    'metric' => $this->urlProvider->getUrlByName($result->getName())->getMetricUuid(),
91
                    'value' => $result->getReponseTime() * 1000,
92
                    'datetime' => date('Y-m-d H:i:s'),
93
                ]);
94
            } catch (\Exception $e) {
95
                echo sprintf('There is an error %s', $e->getMessage());
96
            }
97
        }
98
    }
99
}
100