Skip to content

Commit 69f3289

Browse files
Merge pull request #381 from JLG-WOCFR-DEV/codex/introduce-services-in-scanner-directory
Refactor image URL normalization services
2 parents 442e892 + 8fc3126 commit 69f3289

File tree

8 files changed

+698
-116
lines changed

8 files changed

+698
-116
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
if (!class_exists('BlcImageHostPolicyResult')) {
4+
class BlcImageHostPolicyResult {
5+
/**
6+
* @var string
7+
*/
8+
private $host;
9+
10+
/**
11+
* @var bool
12+
*/
13+
private $matchesSite;
14+
15+
/**
16+
* @var bool
17+
*/
18+
private $matchesUpload;
19+
20+
public function __construct($host, $matches_site, $matches_upload) {
21+
$this->host = (string) $host;
22+
$this->matchesSite = (bool) $matches_site;
23+
$this->matchesUpload = (bool) $matches_upload;
24+
}
25+
26+
/**
27+
* @return bool
28+
*/
29+
public function isValid() {
30+
return $this->host !== '';
31+
}
32+
33+
/**
34+
* @return string
35+
*/
36+
public function getHost() {
37+
return $this->host;
38+
}
39+
40+
/**
41+
* @return bool
42+
*/
43+
public function isSiteHost() {
44+
return $this->matchesSite;
45+
}
46+
47+
/**
48+
* @return bool
49+
*/
50+
public function isUploadHost() {
51+
return $this->matchesUpload;
52+
}
53+
54+
/**
55+
* @return bool
56+
*/
57+
public function isRemoteHost() {
58+
return $this->isValid() && !$this->matchesSite && !$this->matchesUpload;
59+
}
60+
}
61+
}
62+
63+
if (!class_exists('BlcImageHostPolicy')) {
64+
class BlcImageHostPolicy {
65+
/**
66+
* @var string
67+
*/
68+
private $normalizedSiteHost;
69+
70+
/**
71+
* @var string
72+
*/
73+
private $uploadBaseurlHost;
74+
75+
public function __construct($normalized_site_host, $upload_baseurl_host) {
76+
$this->normalizedSiteHost = (string) $normalized_site_host;
77+
$this->uploadBaseurlHost = (string) $upload_baseurl_host;
78+
}
79+
80+
/**
81+
* Analyse a normalized URL and classify its host.
82+
*
83+
* @param string $normalized_url
84+
*
85+
* @return BlcImageHostPolicyResult
86+
*/
87+
public function analyzeUrl($normalized_url) {
88+
$host_raw = parse_url((string) $normalized_url, PHP_URL_HOST);
89+
$host = is_string($host_raw) ? blc_normalize_remote_host($host_raw) : '';
90+
91+
$matches_site = ($host !== '' && $this->normalizedSiteHost !== '' && $host === $this->normalizedSiteHost);
92+
$matches_upload = ($host !== '' && $this->uploadBaseurlHost !== '' && $host === $this->uploadBaseurlHost);
93+
94+
return new BlcImageHostPolicyResult($host, $matches_site, $matches_upload);
95+
}
96+
}
97+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
if (!class_exists('BlcImageNormalizationResult')) {
4+
class BlcImageNormalizationResult {
5+
/**
6+
* @var bool
7+
*/
8+
private $successful;
9+
10+
/**
11+
* @var string
12+
*/
13+
private $reason;
14+
15+
/**
16+
* @var string
17+
*/
18+
private $originalUrl;
19+
20+
/**
21+
* @var string
22+
*/
23+
private $normalizedUrl;
24+
25+
/**
26+
* @var string
27+
*/
28+
private $filePath;
29+
30+
/**
31+
* @var string
32+
*/
33+
private $decodedRelativePath;
34+
35+
/**
36+
* @var bool
37+
*/
38+
private $remoteUploadCandidate;
39+
40+
private function __construct($successful, $reason, $original_url, $normalized_url, $file_path, $decoded_relative_path, $remote_upload_candidate) {
41+
$this->successful = (bool) $successful;
42+
$this->reason = (string) $reason;
43+
$this->originalUrl = (string) $original_url;
44+
$this->normalizedUrl = (string) $normalized_url;
45+
$this->filePath = (string) $file_path;
46+
$this->decodedRelativePath = (string) $decoded_relative_path;
47+
$this->remoteUploadCandidate = (bool) $remote_upload_candidate;
48+
}
49+
50+
/**
51+
* @param string $original_url
52+
* @param string $normalized_url
53+
* @param string $file_path
54+
* @param string $decoded_relative_path
55+
* @param bool $remote_upload_candidate
56+
*
57+
* @return self
58+
*/
59+
public static function success($original_url, $normalized_url, $file_path, $decoded_relative_path, $remote_upload_candidate) {
60+
return new self(true, '', $original_url, $normalized_url, $file_path, $decoded_relative_path, $remote_upload_candidate);
61+
}
62+
63+
/**
64+
* @param string $reason
65+
* @param string $original_url
66+
* @param string $normalized_url
67+
*
68+
* @return self
69+
*/
70+
public static function failure($reason, $original_url = '', $normalized_url = '') {
71+
return new self(false, $reason, $original_url, $normalized_url, '', '', false);
72+
}
73+
74+
/**
75+
* @return bool
76+
*/
77+
public function isSuccessful() {
78+
return $this->successful;
79+
}
80+
81+
/**
82+
* @return string
83+
*/
84+
public function getReason() {
85+
return $this->reason;
86+
}
87+
88+
/**
89+
* @return string
90+
*/
91+
public function getOriginalUrl() {
92+
return $this->originalUrl;
93+
}
94+
95+
/**
96+
* @return string
97+
*/
98+
public function getNormalizedUrl() {
99+
return $this->normalizedUrl;
100+
}
101+
102+
/**
103+
* @return string
104+
*/
105+
public function getFilePath() {
106+
return $this->filePath;
107+
}
108+
109+
/**
110+
* @return string
111+
*/
112+
public function getDecodedRelativePath() {
113+
return $this->decodedRelativePath;
114+
}
115+
116+
/**
117+
* @return bool
118+
*/
119+
public function isRemoteUploadCandidate() {
120+
return $this->remoteUploadCandidate;
121+
}
122+
}
123+
}

0 commit comments

Comments
 (0)