GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ResponseValidator   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 10
Bugs 2 Features 4
Metric Value
wmc 25
c 10
b 2
f 4
lcom 1
cbo 1
dl 0
loc 118
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A validateSessionStatsGetResponse() 0 17 4
C validate() 0 25 8
A validateGetResponse() 0 11 3
B validateAddResponse() 0 14 5
A validateSessionGetResponse() 0 10 2
A map() 0 5 1
A validateFreeSpaceGetResponse() 0 10 2
1
<?php
2
namespace Transmission\Util;
3
4
/**
5
 * @author Ramon Kleiss <[email protected]>
6
 */
7
class ResponseValidator
8
{
9
    /**
10
     * @param  string    $method
11
     * @param  \stdClass $response
12
     * @return \stdClass
13
     */
14
    public static function validate($method, \stdClass $response)
15
    {
16
        if (!isset($response->result)) {
17
            throw new \RuntimeException('Invalid response received from Transmission');
18
        }
19
20
        if (!in_array($response->result, array('success', 'duplicate torrent'))) {
21
            throw new \RuntimeException(sprintf(
22
                'An error occured: "%s"', $response->result
23
            ));
24
        }
25
26
        switch ($method) {
27
            case 'torrent-get':
28
                return self::validateGetResponse($response);
29
            case 'torrent-add':
30
                return self::validateAddResponse($response);
31
            case 'session-get':
32
                return self::validateSessionGetResponse($response);
33
            case 'session-stats':
34
                return self::validateSessionStatsGetResponse($response);
35
            case 'free-space':
36
                return self::validateFreeSpaceGetResponse($response);
37
        }
38
    }
39
40
    /**
41
     * @param  \stdClass         $response
42
     * @throws \RuntimeException
43
     */
44
    public static function validateGetResponse(\stdClass $response)
45
    {
46
        if (!isset($response->arguments) ||
47
            !isset($response->arguments->torrents)) {
48
            throw new \RuntimeException(
49
                'Invalid response received from Transmission'
50
            );
51
        }
52
53
        return $response->arguments->torrents;
54
    }
55
56
    /**
57
     * @param  \stdClass         $response
58
     * @throws \RuntimeException
59
     */
60
    public static function validateAddResponse(\stdClass $response)
61
    {
62
        $fields = array('torrent-added', 'torrent-duplicate');
63
64
        foreach ($fields as $field) {
65
            if (isset($response->arguments) &&
66
                isset($response->arguments->$field) &&
67
                count($response->arguments->$field)) {
68
                return $response->arguments->$field;
69
            }
70
        }
71
72
        throw new \RuntimeException('Invalid response received from Transmission');
73
    }
74
75
    public static function validateSessionGetResponse(\stdClass $response)
76
    {
77
        if (!isset($response->arguments)) {
78
            throw new \RuntimeException(
79
                'Invalid response received from Transmission'
80
            );
81
        }
82
83
        return $response->arguments;
84
    }
85
86
    /**
87
     * @param  \stdClass $response
88
     * @return \stdClass
89
     */
90
    public static function validateSessionStatsGetResponse(\stdClass $response)
91
    {
92
        if (!isset($response->arguments)) {
93
            throw new \RuntimeException(
94
                'Invalid response received from Transmission'
95
            );
96
        }
97
        $class='Transmission\\Model\\Stats\\Stats';
98
        foreach (array('cumulative-stats','current-stats') as $property) {
99
            if (property_exists($response->arguments,$property)) {
100
                $instance=self::map($response->arguments->$property,$class);
101
                $response->arguments->$property=$instance;
102
            }
103
        }
104
105
        return $response->arguments;
106
    }
107
108
    private static function map($object,$class)
109
    {
110
        return PropertyMapper::map(new $class(),$object);
111
112
    }
113
114
    public static function validateFreeSpaceGetResponse(\stdClass $response)
115
    {
116
        if (!isset($response->arguments)) {
117
            throw new \RuntimeException(
118
                'Invalid response received from Transmission'
119
            );
120
        }
121
122
        return $response->arguments;
123
    }
124
}
125