Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
66.67% |
16 / 24 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
AllHandler | |
66.67% |
16 / 24 |
|
33.33% |
1 / 3 |
8.81 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
factory | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
run | |
85.71% |
12 / 14 |
|
0.00% |
0 / 1 |
5.07 |
1 | <?php |
2 | |
3 | namespace MediaWiki\SecurityApi\Rest\Feed; |
4 | |
5 | use Config; |
6 | use MediaWiki\Http\HttpRequestFactory; |
7 | use MediaWiki\Permissions\PermissionManager; |
8 | use MediaWiki\Rest\LocalizedHttpException; |
9 | use MediaWiki\Rest\Response; |
10 | use MediaWiki\Rest\SimpleHandler; |
11 | use MediaWiki\User\UserIdentity; |
12 | use RequestContext; |
13 | use Wikimedia\Message\MessageValue; |
14 | |
15 | class AllHandler extends SimpleHandler { |
16 | /** @var Config */ |
17 | private $config; |
18 | |
19 | /** @var HttpRequestFactory */ |
20 | private $httpRequestFactory; |
21 | |
22 | /** @var PermissionManager */ |
23 | private $permissionManager; |
24 | |
25 | /** @var UserIdentity */ |
26 | private $user; |
27 | |
28 | /** |
29 | * @param Config $config |
30 | * @param HttpRequestFactory $httpRequestFactory |
31 | * @param PermissionManager $permissionManager |
32 | * @param UserIdentity $user |
33 | */ |
34 | public function __construct( |
35 | Config $config, |
36 | HttpRequestFactory $httpRequestFactory, |
37 | PermissionManager $permissionManager, |
38 | UserIdentity $user |
39 | ) { |
40 | $this->config = $config; |
41 | $this->httpRequestFactory = $httpRequestFactory; |
42 | $this->permissionManager = $permissionManager; |
43 | $this->user = $user; |
44 | } |
45 | |
46 | /** |
47 | * @param Config $config |
48 | * @param HttpRequestFactory $httpRequestFactory |
49 | * @param PermissionManager $permissionManager |
50 | * @return self |
51 | */ |
52 | public static function factory( |
53 | Config $config, |
54 | HttpRequestFactory $httpRequestFactory, |
55 | PermissionManager $permissionManager |
56 | ) { |
57 | return new self( |
58 | $config, |
59 | $httpRequestFactory, |
60 | $permissionManager, |
61 | RequestContext::getMain()->getUser() |
62 | ); |
63 | } |
64 | |
65 | /** |
66 | * Get all info on ips provided by ipoid/feed |
67 | * |
68 | * @return Response |
69 | */ |
70 | public function run(): Response { |
71 | if ( |
72 | !$this->permissionManager->userHasRight( $this->user, 'securityapi-ipoid-feed' ) |
73 | ) { |
74 | throw new LocalizedHttpException( |
75 | new MessageValue( 'securityapi-rest-access-denied' ), $this->user->isRegistered() ? 403 : 401 ); |
76 | } |
77 | |
78 | $baseUrl = $this->config->get( 'SecurityApiIpoidUrl' ); |
79 | if ( !$baseUrl ) { |
80 | throw new LocalizedHttpException( |
81 | new MessageValue( 'securityapi-invalid-url' ), 400 ); |
82 | } |
83 | |
84 | // Get response from ipoid |
85 | $url = $baseUrl . '/feed/v1/all'; |
86 | $req = $this->httpRequestFactory->create( $url, [ 'method' => 'GET' ] ); |
87 | $response = $req->execute(); |
88 | |
89 | if ( !$response->isOK() ) { |
90 | throw new LocalizedHttpException( |
91 | new MessageValue( 'securityapi-rest-error' ), $response->getErrors()[0]['params'][0] ); |
92 | } |
93 | |
94 | return $this->getResponseFactory()->createJson( json_decode( $req->getContent() ) ); |
95 | } |
96 | } |