|
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\Scanner\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 data tokenization has failed, ex. some property path is not found in response |
|
25
|
|
|
*/ |
|
26
|
|
|
class TokenizationFailedException extends RuntimeException implements AnthillBundleExceptionInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Default error message |
|
30
|
|
|
* |
|
31
|
|
|
* @const string |
|
32
|
|
|
*/ |
|
33
|
|
|
public const MESSAGE = 'Failed to extract vacancy data from website response.'; |
|
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
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct( |
|
46
|
|
|
string $message = self::MESSAGE, |
|
47
|
|
|
int $code = Response::HTTP_INTERNAL_SERVER_ERROR, |
|
48
|
|
|
Throwable $previous = null |
|
49
|
|
|
) { |
|
50
|
|
|
parent::__construct($message, $code, $previous); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Returns exception with previous one |
|
55
|
|
|
* |
|
56
|
|
|
* @param Throwable $previous Previous exception |
|
57
|
|
|
* |
|
58
|
|
|
* @return TokenizationFailedException |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function withPrevious(Throwable $previous): TokenizationFailedException |
|
61
|
|
|
{ |
|
62
|
|
|
return new static(self::MESSAGE, Response::HTTP_INTERNAL_SERVER_ERROR, $previous); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Returns exception with context of missed key or path in response from website |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $needle Key or path that is missed in response data structure |
|
69
|
|
|
* |
|
70
|
|
|
* @return TokenizationFailedException |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function unexpectedResponse(string $needle): TokenizationFailedException |
|
73
|
|
|
{ |
|
74
|
|
|
$message = str_replace('{needle}', $needle, self::MESSAGE_UNEXPECTED_RESPONSE); |
|
75
|
|
|
|
|
76
|
|
|
return new static($message); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|