Passed
Push — master ( 1199e5...c7b7ee )
by Blizzz
11:21 queued 12s
created
lib/private/Updater/ChangesCheck.php 1 patch
Indentation   +137 added lines, -137 removed lines patch added patch discarded remove patch
@@ -32,141 +32,141 @@
 block discarded – undo
32 32
 use OCP\ILogger;
33 33
 
34 34
 class ChangesCheck {
35
-	/** @var IClientService */
36
-	protected $clientService;
37
-	/** @var ChangesMapper */
38
-	private $mapper;
39
-	/** @var ILogger */
40
-	private $logger;
41
-
42
-	const RESPONSE_NO_CONTENT = 0;
43
-	const RESPONSE_USE_CACHE = 1;
44
-	const RESPONSE_HAS_CONTENT = 2;
45
-
46
-	public function __construct(IClientService $clientService, ChangesMapper $mapper, ILogger $logger) {
47
-		$this->clientService = $clientService;
48
-		$this->mapper = $mapper;
49
-		$this->logger = $logger;
50
-	}
51
-
52
-	/**
53
-	 * @throws DoesNotExistException
54
-	 */
55
-	public function getChangesForVersion(string $version): array {
56
-		$version = $this->normalizeVersion($version);
57
-		$changesInfo = $this->mapper->getChanges($version);
58
-		$changesData = json_decode($changesInfo->getData(), true);
59
-		if(empty($changesData)) {
60
-			throw new DoesNotExistException();
61
-		}
62
-		return $changesData;
63
-	}
64
-
65
-	/**
66
-	 * @throws \Exception
67
-	 */
68
-	public function check(string $uri, string $version): array {
69
-		try {
70
-			$version = $this->normalizeVersion($version);
71
-			$changesInfo = $this->mapper->getChanges($version);
72
-			if($changesInfo->getLastCheck() + 1800 > time()) {
73
-				return json_decode($changesInfo->getData(), true);
74
-			}
75
-		} catch (DoesNotExistException $e) {
76
-			$changesInfo = new ChangesResult();
77
-		}
78
-
79
-		$response = $this->queryChangesServer($uri, $changesInfo);
80
-
81
-		switch($this->evaluateResponse($response)) {
82
-			case self::RESPONSE_NO_CONTENT:
83
-				return [];
84
-			case self::RESPONSE_USE_CACHE:
85
-				return json_decode($changesInfo->getData(), true);
86
-			case self::RESPONSE_HAS_CONTENT:
87
-			default:
88
-				$data = $this->extractData($response->getBody());
89
-				$changesInfo->setData(json_encode($data));
90
-				$changesInfo->setEtag($response->getHeader('Etag'));
91
-				$this->cacheResult($changesInfo, $version);
92
-
93
-				return $data;
94
-		}
95
-	}
96
-
97
-	protected function evaluateResponse(IResponse $response): int {
98
-		if($response->getStatusCode() === 304) {
99
-			return self::RESPONSE_USE_CACHE;
100
-		} else if($response->getStatusCode() === 404) {
101
-			return self::RESPONSE_NO_CONTENT;
102
-		} else if($response->getStatusCode() === 200) {
103
-			return self::RESPONSE_HAS_CONTENT;
104
-		}
105
-		$this->logger->debug('Unexpected return code {code} from changelog server', [
106
-			'app' => 'core',
107
-			'code' => $response->getStatusCode(),
108
-		]);
109
-		return self::RESPONSE_NO_CONTENT;
110
-	}
111
-
112
-	protected function cacheResult(ChangesResult $entry, string $version) {
113
-		if($entry->getVersion() === $version) {
114
-			$this->mapper->update($entry);
115
-		} else {
116
-			$entry->setVersion($version);
117
-			$this->mapper->insert($entry);
118
-		}
119
-	}
120
-
121
-	/**
122
-	 * @throws \Exception
123
-	 */
124
-	protected function queryChangesServer(string $uri, ChangesResult $entry): IResponse {
125
-		$headers = [];
126
-		if($entry->getEtag() !== '') {
127
-			$headers['If-None-Match'] = [$entry->getEtag()];
128
-		}
129
-
130
-		$entry->setLastCheck(time());
131
-		$client = $this->clientService->newClient();
132
-		return $client->get($uri, [
133
-			'headers' => $headers,
134
-		]);
135
-	}
136
-
137
-	protected function extractData($body):array {
138
-		$data = [];
139
-		if ($body) {
140
-			$loadEntities = libxml_disable_entity_loader(true);
141
-			$xml = @simplexml_load_string($body);
142
-			libxml_disable_entity_loader($loadEntities);
143
-			if ($xml !== false) {
144
-				$data['changelogURL'] = (string)$xml->changelog['href'];
145
-				$data['whatsNew'] = [];
146
-				foreach($xml->whatsNew as $infoSet) {
147
-					$data['whatsNew'][(string)$infoSet['lang']] = [
148
-						'regular' => (array)$infoSet->regular->item,
149
-						'admin' => (array)$infoSet->admin->item,
150
-					];
151
-				}
152
-			} else {
153
-				libxml_clear_errors();
154
-			}
155
-		}
156
-		return $data;
157
-	}
158
-
159
-	/**
160
-	 * returns a x.y.z form of the provided version. Extra numbers will be
161
-	 * omitted, missing ones added as zeros.
162
-	 */
163
-	public function normalizeVersion(string $version): string {
164
-		$versionNumbers = array_slice(explode('.', $version), 0, 3);
165
-		$versionNumbers[0] = $versionNumbers[0] ?: '0'; // deal with empty input
166
-		while(count($versionNumbers) < 3) {
167
-			// changelog server expects x.y.z, pad 0 if it is too short
168
-			$versionNumbers[] = 0;
169
-		}
170
-		return implode('.', $versionNumbers);
171
-	}
35
+    /** @var IClientService */
36
+    protected $clientService;
37
+    /** @var ChangesMapper */
38
+    private $mapper;
39
+    /** @var ILogger */
40
+    private $logger;
41
+
42
+    const RESPONSE_NO_CONTENT = 0;
43
+    const RESPONSE_USE_CACHE = 1;
44
+    const RESPONSE_HAS_CONTENT = 2;
45
+
46
+    public function __construct(IClientService $clientService, ChangesMapper $mapper, ILogger $logger) {
47
+        $this->clientService = $clientService;
48
+        $this->mapper = $mapper;
49
+        $this->logger = $logger;
50
+    }
51
+
52
+    /**
53
+     * @throws DoesNotExistException
54
+     */
55
+    public function getChangesForVersion(string $version): array {
56
+        $version = $this->normalizeVersion($version);
57
+        $changesInfo = $this->mapper->getChanges($version);
58
+        $changesData = json_decode($changesInfo->getData(), true);
59
+        if(empty($changesData)) {
60
+            throw new DoesNotExistException();
61
+        }
62
+        return $changesData;
63
+    }
64
+
65
+    /**
66
+     * @throws \Exception
67
+     */
68
+    public function check(string $uri, string $version): array {
69
+        try {
70
+            $version = $this->normalizeVersion($version);
71
+            $changesInfo = $this->mapper->getChanges($version);
72
+            if($changesInfo->getLastCheck() + 1800 > time()) {
73
+                return json_decode($changesInfo->getData(), true);
74
+            }
75
+        } catch (DoesNotExistException $e) {
76
+            $changesInfo = new ChangesResult();
77
+        }
78
+
79
+        $response = $this->queryChangesServer($uri, $changesInfo);
80
+
81
+        switch($this->evaluateResponse($response)) {
82
+            case self::RESPONSE_NO_CONTENT:
83
+                return [];
84
+            case self::RESPONSE_USE_CACHE:
85
+                return json_decode($changesInfo->getData(), true);
86
+            case self::RESPONSE_HAS_CONTENT:
87
+            default:
88
+                $data = $this->extractData($response->getBody());
89
+                $changesInfo->setData(json_encode($data));
90
+                $changesInfo->setEtag($response->getHeader('Etag'));
91
+                $this->cacheResult($changesInfo, $version);
92
+
93
+                return $data;
94
+        }
95
+    }
96
+
97
+    protected function evaluateResponse(IResponse $response): int {
98
+        if($response->getStatusCode() === 304) {
99
+            return self::RESPONSE_USE_CACHE;
100
+        } else if($response->getStatusCode() === 404) {
101
+            return self::RESPONSE_NO_CONTENT;
102
+        } else if($response->getStatusCode() === 200) {
103
+            return self::RESPONSE_HAS_CONTENT;
104
+        }
105
+        $this->logger->debug('Unexpected return code {code} from changelog server', [
106
+            'app' => 'core',
107
+            'code' => $response->getStatusCode(),
108
+        ]);
109
+        return self::RESPONSE_NO_CONTENT;
110
+    }
111
+
112
+    protected function cacheResult(ChangesResult $entry, string $version) {
113
+        if($entry->getVersion() === $version) {
114
+            $this->mapper->update($entry);
115
+        } else {
116
+            $entry->setVersion($version);
117
+            $this->mapper->insert($entry);
118
+        }
119
+    }
120
+
121
+    /**
122
+     * @throws \Exception
123
+     */
124
+    protected function queryChangesServer(string $uri, ChangesResult $entry): IResponse {
125
+        $headers = [];
126
+        if($entry->getEtag() !== '') {
127
+            $headers['If-None-Match'] = [$entry->getEtag()];
128
+        }
129
+
130
+        $entry->setLastCheck(time());
131
+        $client = $this->clientService->newClient();
132
+        return $client->get($uri, [
133
+            'headers' => $headers,
134
+        ]);
135
+    }
136
+
137
+    protected function extractData($body):array {
138
+        $data = [];
139
+        if ($body) {
140
+            $loadEntities = libxml_disable_entity_loader(true);
141
+            $xml = @simplexml_load_string($body);
142
+            libxml_disable_entity_loader($loadEntities);
143
+            if ($xml !== false) {
144
+                $data['changelogURL'] = (string)$xml->changelog['href'];
145
+                $data['whatsNew'] = [];
146
+                foreach($xml->whatsNew as $infoSet) {
147
+                    $data['whatsNew'][(string)$infoSet['lang']] = [
148
+                        'regular' => (array)$infoSet->regular->item,
149
+                        'admin' => (array)$infoSet->admin->item,
150
+                    ];
151
+                }
152
+            } else {
153
+                libxml_clear_errors();
154
+            }
155
+        }
156
+        return $data;
157
+    }
158
+
159
+    /**
160
+     * returns a x.y.z form of the provided version. Extra numbers will be
161
+     * omitted, missing ones added as zeros.
162
+     */
163
+    public function normalizeVersion(string $version): string {
164
+        $versionNumbers = array_slice(explode('.', $version), 0, 3);
165
+        $versionNumbers[0] = $versionNumbers[0] ?: '0'; // deal with empty input
166
+        while(count($versionNumbers) < 3) {
167
+            // changelog server expects x.y.z, pad 0 if it is too short
168
+            $versionNumbers[] = 0;
169
+        }
170
+        return implode('.', $versionNumbers);
171
+    }
172 172
 }
Please login to merge, or discard this patch.