Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gh 6 #8

Merged
merged 47 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
e815be8
feat: reindex shop orders with new uuid
DEMAxx May 31, 2024
0197bf1
fix: changes in request to core api
DEMAxx May 31, 2024
1832c8e
fix: response check success answer
DEMAxx May 31, 2024
5e5102b
fix: remove not used method
DEMAxx May 31, 2024
9f3c078
fix: site name key change
DEMAxx May 31, 2024
02c3ff5
fix: exchange token to shop token was changed, get shop uuid inside f…
DEMAxx May 31, 2024
aeae10d
fix: cache token in file and get from cache
DEMAxx May 31, 2024
e42e719
fix: get and create api tasks
DEMAxx Jun 3, 2024
c40b6aa
fix: start task as schedule work
DEMAxx Jun 3, 2024
335aa4f
fix: rewrite cache, start events, tasks
DEMAxx Jun 3, 2024
353bb23
fix: task hook add prefix
DEMAxx Jun 3, 2024
de2422d
fix: small fix
DEMAxx Jun 3, 2024
b9d0a7c
feat: unregister events, rewrite api send, additional headers, rewrit…
DEMAxx Jun 4, 2024
df69551
fix: unregister events
DEMAxx Jun 4, 2024
2934250
fix: after review fixes
DEMAxx Jun 5, 2024
e259701
fix: test and fix work of core api
DEMAxx Jun 5, 2024
c75dba7
fix: rewrite classes for optimal work with core api
DEMAxx Jun 6, 2024
baecdb7
fix: remove check event on activate hook
DEMAxx Jun 6, 2024
2c87638
fix: change headers fixed
DEMAxx Jun 6, 2024
f497c68
feat: work with statuses
DEMAxx Jun 6, 2024
2383a4c
fix: remove cache, unschedule events with deactivation plugin, fix ac…
DEMAxx Jun 7, 2024
ff4d149
fix: change send headers logic
DEMAxx Jun 7, 2024
9ff066b
fix: postpone task for server fatal errors answer
DEMAxx Jun 7, 2024
5f6b0b7
feat: change exception to rest for schedule events
DEMAxx Jun 10, 2024
18df8e4
fix: methods witch work with update uuids from core api
DEMAxx Jun 10, 2024
ebb2f81
fix: reschedule task manager event if case api core error
DEMAxx Jun 10, 2024
4a64f54
fix: code style
DEMAxx Jun 10, 2024
7f2fa93
fix: fix after review
DEMAxx Jun 11, 2024
9c82b7a
fix: getPath method doc
DEMAxx Jun 11, 2024
5b6f655
fix: replace work with response logic to transport class
DEMAxx Jun 11, 2024
f5f8fe1
fix: server errors check
DEMAxx Jun 11, 2024
9fbd564
fix: server errors check + rename schedule exceptions
DEMAxx Jun 11, 2024
f781f45
fix: check DOING_CRON const in API exception
DEMAxx Jun 11, 2024
7dbcab4
fix: cache class work with only one file
DEMAxx Jun 11, 2024
4e33774
fix: add core request data as dto model
DEMAxx Jun 11, 2024
7dc1fa4
fix: remove headers data and fix status detect
DEMAxx Jun 11, 2024
e24d810
fix: remove rename task output data, remove Task Contract unnecessary…
DEMAxx Jun 13, 2024
4bf4e3c
fix: remove error collection
DEMAxx Jun 13, 2024
5f82a15
fix: task manager no more extends from Task Contract, type hints, met…
DEMAxx Jun 14, 2024
edf08a5
fix: response only array + error catch as exception
DEMAxx Jun 14, 2024
ce9259b
fix: type and methods doc
DEMAxx Jun 14, 2024
b7b5262
fix: type hint
DEMAxx Jun 14, 2024
3a704da
fix: fixes after review
DEMAxx Jun 17, 2024
ea2400f
fix: exceptions fixed
DEMAxx Jun 17, 2024
ae7b17b
fix: exception add $stopPropagation
DEMAxx Jun 17, 2024
ed84ca0
fix: global view to constants classes
DEMAxx Jun 17, 2024
afc65ca
fix: view mode to constants
DEMAxx Jun 17, 2024
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
58 changes: 58 additions & 0 deletions src/Actions/Schedule/CollectOrders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

vermorag marked this conversation as resolved.
Show resolved Hide resolved
namespace {
defined('ABSPATH') or exit;
}

namespace Cdek\Actions\Schedule {

use Cdek\Contracts\TaskContract;
use Cdek\Model\TaskOutputData;

class CollectOrders extends TaskContract
{
private const ORDERS_LIMIT = 10000;

public static function getName(): string
{
return 'collect-orphaned-orders';
}

public function start(): void
{
$query = new \WC_Order_Query(
[
'orderby' => 'id',
'order' => 'ASC',
'paginate' => true,
'limit' => self::ORDERS_LIMIT,
'return' => 'ids',
],
);

for ($page = 1, $maxPages = 1; $page <= $maxPages; $page++) {
$query->set('page', $page);
$result = $query->get_orders();

$maxPages = $result->max_num_pages;

$response = $this->cdekCoreApi->sendTaskData(
$this->taskId,
new TaskOutputData(
'success',
[
'orders' => array_map(
static fn($order) => (string)$order,
$result->orders,
)
],
$page,
$maxPages
)
);

$this->initData($response);
}
}
}
}
61 changes: 61 additions & 0 deletions src/Actions/Schedule/ReindexOrders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php


namespace {

defined('ABSPATH') or exit;
}

namespace Cdek\Actions\Schedule {

use Cdek\Contracts\TaskContract;
use Cdek\Exceptions\CdekApiException;
use Cdek\Exceptions\CdekScheduledTaskException;
use Cdek\Model\TaskOutputData;
use Cdek\Model\OrderMetaData;
use Cdek\Model\Validate;

class ReindexOrders extends TaskContract
{
public function __construct(string $taskId)
{
parent::__construct($taskId);
$this->initTaskData();
}

public static function getName(): string
{
return 'restore-order-uuids';
}

/**
* @return void
* @throws CdekApiException
* @throws CdekScheduledTaskException
* @throws \JsonException
*/
public function start(): void
{
if (empty($this->getTaskMeta())) {
throw new CdekScheduledTaskException(
'[CDEKDelivery] Failed to get orders meta info',
'cdek_error.core.data',
);
}

foreach ($this->getTaskMeta() as $arOrder) {
OrderMetaData::updateMetaByOrderId(
$arOrder['external_id'],
[
'order_uuid' => $arOrder['id'],
],
);
}

$this->initData($this->cdekCoreApi->sendTaskData(
$this->taskId,
new TaskOutputData('success'),
));
}
}
}
67 changes: 67 additions & 0 deletions src/Cache/FileCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

vermorag marked this conversation as resolved.
Show resolved Hide resolved
namespace {
defined('ABSPATH') or exit;
}

namespace Cdek\Cache
{

use Cdek\Exceptions\CdekApiException;
use Cdek\Loader;

class FileCache
{
private const CACHE_FILE_NAME = '.cache.php';
private static array $store;

public static function getVars(): ?array
{
if(!file_exists(Loader::getPluginPath() . DIRECTORY_SEPARATOR . self::CACHE_FILE_NAME)){
return null;
}

return self::$store[self::CACHE_FILE_NAME] ?? self::$store[self::CACHE_FILE_NAME] = require_once(Loader::getPluginPath() . DIRECTORY_SEPARATOR . self::CACHE_FILE_NAME);
}

/**
* @param array|null $vars
*
* @return void
* @throws CdekApiException
*/
public static function putVars(?array $vars): void
{
if($vars === null){
return;
}

if(file_exists(Loader::getPluginPath() . DIRECTORY_SEPARATOR . self::CACHE_FILE_NAME)){
if(!is_writable(Loader::getPluginPath() . DIRECTORY_SEPARATOR . self::CACHE_FILE_NAME)){
throw new CdekApiException('[CDEKDelivery] Failed check file rights',
'cdek_error.cache.rights',
['path' => Loader::getPluginPath() . DIRECTORY_SEPARATOR . self::CACHE_FILE_NAME],
true);
}
}else{
if(!is_writable(Loader::getPluginPath())){
throw new CdekApiException('[CDEKDelivery] Failed check directory rights',
'cdek_error.cache.rights',
['path' => Loader::getPluginPath()],
true);
}
}


$logFile = fopen( Loader::getPluginPath() . DIRECTORY_SEPARATOR . self::CACHE_FILE_NAME, 'w+');

fwrite($logFile, '<?php return ' . var_export($vars, true) . ';' . PHP_EOL);
fclose($logFile);
}

public static function clear(): void
{
unlink(Loader::getPluginPath() . DIRECTORY_SEPARATOR . self::CACHE_FILE_NAME);
}
}
}
Loading
Loading