Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/code_style.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Code style

on: [ push ]

jobs:
php-cs-fixer:
name: PHP-CS-Fixer
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: PHP-CS-Fixer
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: --allow-risky=yes --dry-run --diff --verbose
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.idea
.DS_Store
composer.lock
.php-cs-fixer.cache
34 changes: 34 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use PhpCsFixer\Finder;
use PhpCsFixer\Config;

$finder = Finder::create()
->in([
'src',
'tests'
]);

$config = new Config();

return $config
->setRules([
'@PSR12' => true,
'strict_param' => true,
'array_syntax' => ['syntax' => 'short'],
'no_trailing_comma_in_singleline' => true,
'no_unused_imports' => true,
'trailing_comma_in_multiline' => [
'elements' => ['arrays'],
],
'blank_line_before_statement' => ['statements' => ['return']],
'braces' => [
'position_after_functions_and_oop_constructs' => 'next',
],
'php_unit_construct' => true,
'php_unit_method_casing' => true,
'php_unit_mock_short_will_return' => true,
'php_unit_test_annotation' => true,
'php_unit_test_case_static_method_calls' => ['call_type' => 'this'],
])
->setFinder($finder);
24 changes: 19 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
.SILENT:

all: test
DC = docker compose run --rm php

all: dependencies lint test

.PHONY: shell sh
shell sh:
docker compose run --rm shell sh
${DC} sh

.PHONY: dependencies
dependencies:
${DC} composer install --no-interaction

.PHONY: test
test:
docker compose run --rm shell ./vendor/bin/phpunit tests
test: dependencies
${DC} composer test

.PHONY: test-legacy
test-legacy:
docker compose run --rm test
${DC} php /app/tests/test.php

.PHONY: build
build:
docker compose build

.PHONY: lint
lint:
${DC} composer lint:show

.PHONY: lint-fix
lint-fix:
${DC} composer lint:fix
16 changes: 15 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@
}
],
"require-dev": {
"phpunit/phpunit": "^9.6"
"phpunit/phpunit": "^9.6",
"friendsofphp/php-cs-fixer": "^3.89"
},
"scripts": {
"test": [
"@test:unit"
],
"test:unit": "./vendor/bin/phpunit tests",
"lint:fix": "./vendor/bin/php-cs-fixer fix --allow-risky=yes",
"lint:show": "@lint:fix --dry-run --diff --verbose"
},
"config": {
"platform": {
"php": "7.4.1"
}
}
}
15 changes: 1 addition & 14 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
services:
shell:
php:
build: .docker
image: comagic-api-dev
pull_policy: never
volumes:
- .:/app

test:
build: .docker
image: comagic-api-dev
pull_policy: never
volumes:
- .:/app
command: [
"sh", "-c",
"composer install --no-interaction && php /app/tests/test.php"
]
10 changes: 6 additions & 4 deletions src/CoMagic/CallApiClient.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace CoMagic;

Expand Down Expand Up @@ -27,8 +29,8 @@ public function __construct(CallApiConfig $config, ?Client $client = null)
$this->client = $client ?? new Client([
'headers' => [
'Accept' => 'application/json',
'Content-type' => 'application/json; charset=UTF-8'
]
'Content-type' => 'application/json; charset=UTF-8',
],
]);

$this->accessToken = $config->getAccessToken();
Expand All @@ -52,7 +54,7 @@ private function refreshAccessToken(): void
'login.user',
[
'login' => $this->login,
'password' => $this->password
'password' => $this->password,
]
);

Expand Down
4 changes: 3 additions & 1 deletion src/CoMagic/CallApiConfig.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace CoMagic;

Expand Down
39 changes: 15 additions & 24 deletions src/CoMagic/RestApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ public function __construct($config)
$this->_client = new Client([
'base_uri' => $this->_entryPoint,
'headers' => [
'Accept' => 'application/json'
]
'Accept' => 'application/json',
],
]);

if (!empty($config['login']) && !empty($config['password']))
{
if (!empty($config['login']) && !empty($config['password'])) {
$this->login($config['login'], $config['password']);
}
}
Expand All @@ -82,15 +81,15 @@ public function login($login, $password)
$payload = [
'form_params' => [
'login' => $login,
'password' => $password
]
'password' => $password,
],
];

$data = $this->_doRequest('login', $payload);

if (isset($data->session_key))
{
if (isset($data->session_key)) {
$this->_sessionKey = $data->session_key;

return true;
}

Expand All @@ -109,18 +108,15 @@ public function __call($method, $arguments)
{
$payload = ['session_key' => $this->_sessionKey];

if (is_null($this->_sessionKey))
{
if (is_null($this->_sessionKey)) {
throw new Exception('You are not logged in');
}

if (!empty($arguments[0]))
{
if (!empty($arguments[0])) {
$payload = array_merge($payload, $arguments[0]);
}

if ($this->_mapMethod($method) === 'GET')
{
if ($this->_mapMethod($method) === 'GET') {
$payload = ['query' => $payload];
}

Expand All @@ -137,8 +133,7 @@ public function __call($method, $arguments)
*/
private function _doRequest($method, $payload)
{
try
{
try {
$response = $this->_client->request(
$this->_mapMethod($method),
$method . '/',
Expand All @@ -147,15 +142,12 @@ private function _doRequest($method, $payload)

$responseBody = json_decode($response->getBody());

if (!$responseBody->success)
{
if (!$responseBody->success) {
throw new Exception($responseBody->message);
}

return $responseBody->data;
}
catch (TransferException $e)
{
} catch (TransferException $e) {
throw new Exception($e->getMessage());
}
}
Expand All @@ -168,8 +160,7 @@ private function _doRequest($method, $payload)
*/
private function _mapMethod($name)
{
switch ($name)
{
switch ($name) {
case 'create_agent_customer':
case 'create_site':
case 'create_ac_condition_group':
Expand All @@ -189,4 +180,4 @@ private function _mapMethod($name)
return 'GET';
}
}
}
}
6 changes: 4 additions & 2 deletions tests/CallApiClientTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

use CoMagic\CallApiClient;
use CoMagic\CallApiConfig;
Expand Down Expand Up @@ -101,7 +103,7 @@ public function apiCallsProvider(): array
[
'tag_id' => 456,
'tag_name' => 'Целевой',
]
],
],
'legs' => [
[
Expand Down
4 changes: 3 additions & 1 deletion tests/CallApiConfigTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

use CoMagic\CallApiConfig;
use PHPUnit\Framework\TestCase;
Expand Down