• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

eliashaeussler / cpanel-requests / 8845147324

26 Apr 2024 08:10AM CUT coverage: 99.497%. Remained the same
8845147324

push

github

web-flow
Merge pull request #229 from eliashaeussler/renovate/paambaati-codeclimate-action-6.x

[TASK] Update paambaati/codeclimate-action action to v6

396 of 398 relevant lines covered (99.5%)

9.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/Application/CPanel.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Composer package "eliashaeussler/cpanel-requests".
7
 *
8
 * Copyright (C) 2020-2024 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace EliasHaeussler\CpanelRequests\Application;
25

26
use EliasHaeussler\CpanelRequests\Exception;
27
use EliasHaeussler\CpanelRequests\Http;
28
use EliasHaeussler\CpanelRequests\Resource;
29
use GuzzleHttp\HandlerStack;
30
use GuzzleHttp\MessageFormatter;
31
use GuzzleHttp\Middleware;
32
use GuzzleHttp\Psr7;
33
use Monolog\Handler;
34
use Monolog\Level;
35
use Monolog\Logger;
36
use Psr\Http\Client;
37
use Psr\Http\Message;
38

39
/**
40
 * General cPanel class.
41
 *
42
 * @author Elias Häußler <elias@haeussler.dev>
43
 * @license GPL-3.0-or-later
44
 */
45
final class CPanel
46
{
47
    private readonly Message\UriInterface $baseUri;
48
    private readonly Http\Response\ResponseFactory $responseFactory;
49
    private readonly Resource\File $logFile;
50

51
    public function __construct(
2✔
52
        private readonly Authorization\AuthorizationInterface $authorization,
53
        private readonly string $host,
54
        private readonly int $port = 2083,
55
        private readonly Http\Protocol $protocol = Http\Protocol::Https,
56
    ) {
57
        $this->baseUri = $this->buildBaseUri();
2✔
58
        $this->responseFactory = new Http\Response\ResponseFactory();
2✔
59
        $this->logFile = Resource\Log::create();
2✔
60
    }
61

62
    /**
63
     * @param non-empty-string     $module
64
     * @param non-empty-string     $function
65
     * @param array<string, mixed> $parameters
66
     *
67
     * @see https://api.docs.cpanel.net/cpanel/introduction
68
     */
69
    public function api(string $module, string $function, array $parameters = []): Http\Response\ResponseInterface
2✔
70
    {
71
        // Create logger
72
        $requestId = uniqid('request-');
2✔
73
        $logger = new Logger($requestId);
2✔
74
        $logger->pushHandler(new Handler\StreamHandler($this->logFile->getPathname(), Level::Warning));
2✔
75

76
        // Create handler stack
77
        $stack = HandlerStack::create();
2✔
78
        $stack->push(Middleware::log($logger, new MessageFormatter()));
2✔
79
        $options = [
2✔
80
            'handler' => $stack,
2✔
81
        ];
2✔
82

83
        try {
84
            $request = new Http\Request\ApiRequest($this->baseUri, $module, $function, $parameters);
2✔
85
            $response = $this->authorization->sendAuthorizedRequest('GET', $request, $options);
2✔
86

87
            return $this->responseFactory->createFromResponse($response);
1✔
88
        } catch (Client\ClientExceptionInterface|Exception\Exception $exception) {
1✔
89
            throw Exception\RequestFailedException::create($exception);
1✔
90
        }
91
    }
92

93
    private function buildBaseUri(): Message\UriInterface
2✔
94
    {
95
        return (new Psr7\Uri())
2✔
96
            ->withScheme($this->protocol->value)
2✔
97
            ->withHost($this->host)
2✔
98
            ->withPort($this->port)
2✔
99
        ;
2✔
100
    }
101
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc