1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Veslo project <https://github.com/symfony-doge/veslo>. |
5
|
|
|
* |
6
|
|
|
* (C) 2019 Pavel Petrov <[email protected]>. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 GPL-3.0 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
declare(strict_types=1); |
15
|
|
|
|
16
|
|
|
namespace Veslo\AnthillBundle\Exception\Vacancy\Roadmap\Strategy; |
17
|
|
|
|
18
|
|
|
use RuntimeException; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
use Throwable; |
21
|
|
|
use Veslo\AnthillBundle\Exception\AnthillBundleExceptionInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Will be thrown if vacancy lookup algorithm on website has failed |
25
|
|
|
*/ |
26
|
|
|
class LookupFailedException extends RuntimeException implements AnthillBundleExceptionInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Default error message |
30
|
|
|
* |
31
|
|
|
* @const string |
32
|
|
|
*/ |
33
|
|
|
public const MESSAGE = 'Failed to lookup vacancy on website.'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Error message if response from website is not as expected |
37
|
|
|
* |
38
|
|
|
* @const string |
39
|
|
|
*/ |
40
|
|
|
public const MESSAGE_UNEXPECTED_RESPONSE = "Bastardi! Dove sono le offerte di lavoro? ('{needle}' is missed)"; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Error message if website response is unstable during a series of consecutive requests |
44
|
|
|
* Some algorithms are based on recursive calls with consistency checks |
45
|
|
|
* |
46
|
|
|
* @const string |
47
|
|
|
*/ |
48
|
|
|
public const MESSAGE_PROVIDER_IS_UNSTABLE = "Vacancy provider '{providerUri}' is unstable. Lets try again later."; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function __construct( |
54
|
|
|
string $message = self::MESSAGE, |
55
|
|
|
int $code = Response::HTTP_INTERNAL_SERVER_ERROR, |
56
|
|
|
Throwable $previous = null |
57
|
|
|
) { |
58
|
|
|
parent::__construct($message, $code, $previous); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns exception with previous one |
63
|
|
|
* |
64
|
|
|
* @param Throwable $previous Previous exception |
65
|
|
|
* |
66
|
|
|
* @return LookupFailedException |
67
|
|
|
*/ |
68
|
|
|
public static function withPrevious(Throwable $previous): LookupFailedException |
69
|
|
|
{ |
70
|
|
|
return new static(self::MESSAGE, Response::HTTP_INTERNAL_SERVER_ERROR, $previous); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns exception with context of missed key or path in response from website |
75
|
|
|
* |
76
|
|
|
* @param string $needle Key or path that is missed in response data structure |
77
|
|
|
* |
78
|
|
|
* @return LookupFailedException |
79
|
|
|
*/ |
80
|
|
|
public static function unexpectedResponse(string $needle): LookupFailedException |
81
|
|
|
{ |
82
|
|
|
$message = str_replace('{needle}', $needle, self::MESSAGE_UNEXPECTED_RESPONSE); |
83
|
|
|
|
84
|
|
|
return new static($message); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns exception in context of unstable response from vacancy provider |
89
|
|
|
* |
90
|
|
|
* @param string $providerUri Vacancy provider's URI |
91
|
|
|
* |
92
|
|
|
* @return LookupFailedException |
93
|
|
|
*/ |
94
|
|
|
public static function providerIsUnstable(string $providerUri): LookupFailedException |
95
|
|
|
{ |
96
|
|
|
$message = str_replace('{providerUri}', $providerUri, self::MESSAGE_PROVIDER_IS_UNSTABLE); |
97
|
|
|
|
98
|
|
|
return new static($message); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|