1 | <?php |
||
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) |
||
107 | |||
108 | private static function map($object,$class) |
||
109 | { |
||
110 | return PropertyMapper::map(new $class(),$object); |
||
111 | |||
113 | |||
114 | public static function validateFreeSpaceGetResponse(\stdClass $response) |
||
124 | } |
||
125 |