Completed
Push — master ( 10d98c...e860da )
by Vítězslav
05:36
created

Hooks::refresh()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * FlexiPeeHP - WebHooks.
4
 *
5
 * @link https://www.flexibee.eu/api/dokumentace/ref/web-hooks WebHooks Reference
6
 * @author     Vítězslav Dvořák <[email protected]>
7
 * @copyright  (C) 2015,2016 Spoje.Net
8
 */
9
10
namespace FlexiPeeHP;
11
12
/**
13
 * Hooky pro ChangesAPI
14
 *
15
 * @note Tato položka nemá dostupné položky evidence
16
 */
17
class Hooks extends FlexiBeeRW
18
{
19
    /**
20
     * Evidence užitá objektem.
21
     *
22
     * @var string
23
     */
24
    public $evidence = 'hooks';
25
26
    /**
27
     * Zaregistruje WebHook
28 1
     *
29
     * @param string $url URL skript přímající WebHook
30 1
     * @param string $format json|xml formát přenášených dat
31 1
     * @return boolean výsledek požadavku
32
     */
33 1
    public function register($url, $format = 'json')
34 1
    {
35 1
        $this->setDataValue('url', $url);
36 1
        $this->setDataValue('format', strtoupper($format));
37 1
38
        $hooks = $this->getAllFromFlexibee();
39 1
        if (count($hooks)) {
40
            foreach ($hooks as $hook) {
41 1
                if (is_array($hook) && array_key_exists('url', $hook)) {
42 1
                    if ($hook['url'] == $url) {
43
                        $this->addStatusMessage(_('Url allready registered'),
44
                            'warning');
45
                        return false;
46
                    }
47
                }
48
            }
49
        }
50 1
        $this->performRequest('hooks.xml?'.http_build_query($this->getData()),
51
            'PUT', 'xml');
52 1
53
        return $this->lastResponseCode === 200;
54
    }
55
56
    /**
57
     * Aktualizuje WebHook (penalty = 0)
58
     * 
59
     * @param int $id
60
     */
61
    public function refresh($id)
62
    {
63
        $this->performRequest('hooks/'.$id.'/retry', 'PUT');
64
        return $this->lastResponseCode === 200;
65
    }
66
67
    /**
68
     * Odregistruje webhook
69
     *
70
     * @param int $id číslo záznamu
71
     */
72
    public function unregister($id)
73
    {
74
        return $this->deleteFromFlexiBee($id);
75
    }
76
77
    /**
78
     * Test if given record exists in FlexiBee.
79
     *
80
     * @param array $data
81
     * @return null Unsupported evidence
82
     */
83
    public function recordExists($data = null)
84
    {
85
        return null;
86
    }
87
88
}
89