Skip to content

Commit

Permalink
connection got update() method
Browse files Browse the repository at this point in the history
  • Loading branch information
hokoo committed Oct 21, 2024
1 parent 672c44e commit 2b7bacc
Show file tree
Hide file tree
Showing 16 changed files with 325 additions and 174 deletions.
11 changes: 5 additions & 6 deletions php-wp-unit.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="tests/wp_bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
verbose="true"
>
convertNoticesToExceptions="false"
convertWarningsToExceptions="false"
beStrictAboutTestsThatDoNotTestAnything="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<php>
<const name="DOING_TESTS" value="1" />
<const name="CLIENT_NAME" value="client-test" />
Expand Down
14 changes: 7 additions & 7 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
beStrictAboutTestsThatDoNotTestAnything="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
beStrictAboutTestsThatDoNotTestAnything="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage includeUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
Expand Down
2 changes: 1 addition & 1 deletion src/Abstracts/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class Connection implements IArrayConvertable
{
public ?int $id = 0;
protected ?string $title;
public ?string $title;
public string $relation = '';
public int $from;
public int $to;
Expand Down
14 changes: 0 additions & 14 deletions src/Abstracts/IQuery.php

This file was deleted.

14 changes: 6 additions & 8 deletions src/Abstracts/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,25 @@

namespace iTRON\wpConnections\Abstracts;

use iTRON\wpConnections\MetaCollection;
use iTRON\wpConnections\Query;
use iTRON\wpConnections\ConnectionCollection;
use iTRON\wpConnections\Exceptions\ConnectionWrongData;
use iTRON\wpConnections\Query\Connection;

abstract class Storage
{
/**
* @param Connection $connectionQuery
* @param Query\Connection $connectionQuery
* @throws ConnectionWrongData
*
* @return int Connection ID
*/
abstract public function createConnection(Query\Connection $connectionQuery): int;

/**
* @param Query\Connection $connectionQuery
* @return bool Successful or not.
*
* @throws ConnectionWrongData
*/
abstract public function updateConnection(Query\Connection $connectionQuery): bool;
abstract public function updateConnection(Connection $connection): bool;

/**
* Deletes connections by set of connection IDs.
Expand Down Expand Up @@ -64,11 +61,12 @@ abstract public function findConnections(Query\Connection $params): ConnectionCo
* Only adds meta fields to the DB.
*
* @param int $objectID
* @param Query\MetaCollection $metaQuery
* @param Query\MetaCollection $metaCollection
*
* @return void
* @throws ConnectionWrongData
*/
abstract public function addConnectionMeta(int $objectID, Query\MetaCollection $metaQuery);
abstract public function addConnectionMeta(int $objectID, MetaCollection $metaCollection): void;

/**
* @throws ConnectionWrongData
Expand Down
43 changes: 26 additions & 17 deletions src/ClientRestApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use iTRON\wpConnections\Exceptions\ClientRegisterFail;
use iTRON\wpConnections\Exceptions\ConnectionNotFound;
use iTRON\wpConnections\Exceptions\Exception;
use iTRON\wpConnections\Exceptions\RelationNotFound;
use iTRON\wpConnections\RestResponse\CollectionItem;
use Ramsey\Collection\Exception\OutOfBoundsException;
use WP_Error;
Expand Down Expand Up @@ -100,31 +101,39 @@ public function restUpdateConnection(WP_REST_Request $request)
*/
public function restUpdateConnectionMeta(WP_REST_Request $request)
{
$queryConnection = new Query\Connection();
$queryConnection->set('id', $request->get_param('connectionID'));
try {
$queryConnection = new Query\Connection();
$queryConnection->id = $request->get_param('connectionID');

if ('PUT' === $request->get_method()) {
$queryConnection->meta->setIsUpdate(false);
}
$found = $this->getClient()->getRelation($request->get_param('relation'))->findConnections($queryConnection);

$queryConnection->meta->fromArray((array) $request->get_param('meta'));
if ($found->isEmpty()) {
return rest_ensure_response($this->getError(new ConnectionNotFound()));
}

if ('PATCH' === $request->get_method()) {
$queryConnection->meta->map(
function ($item) {
/** @var Query\Connection $item */
$item->setIsUpdate(false);
}
);
}
$connection = $found->first();

try {
$result = $this->getClient()->getRelation($request->get_param('relation'))->updateConnectionMeta($queryConnection);
if ('PUT' === $request->get_method()) {
$connection->meta->clear();
}

if ('PATCH' === $request->get_method()) {
$filtered_meta = $connection->meta->filter(function (Meta $meta) use ($request) {
return ! in_array($meta->getKey(), array_column($request->get_param('meta'), 'key'));
});

$connection->meta->clear();
$connection->meta->fromArray($filtered_meta->toArray());
}

$connection->meta->fromArray((array) $request->get_param('meta'));

$connection->update();
} catch (Exception $e) {
return rest_ensure_response($this->getError($e));
}

return [ 'updated' => $result ];
return rest_ensure_response([ 'updated' => $connection ]);
}

public function restDeleteConnectionMeta(WP_REST_Request $request)
Expand Down
24 changes: 21 additions & 3 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace iTRON\wpConnections;

use iTRON\wpConnections\Exceptions\ConnectionWrongData;

class Connection extends Abstracts\Connection
{
use ClientInterface;
Expand All @@ -19,10 +21,22 @@ public function __clone()
}

/**
* Saves instance to DB
* Saves instance to DB.
* Cannot be used to create new instance, only to update existing.
* This method overrides fields in a storage, not appends, and deletes all meta fields in a storage before saving.
*
* @throws ConnectionWrongData
*/
public function save()
public function update(): void
{
if (empty($this->id)) {
throw new ConnectionWrongData('Cannot update uninitialized connection', 304);
}

$this->getClient()->getStorage()->updateConnection($this);

$this->getClient()->getStorage()->removeConnectionMeta($this->id, new Query\MetaCollection());
$this->getClient()->getStorage()->addConnectionMeta($this->id, $this->meta);
}

/**
Expand All @@ -39,8 +53,12 @@ protected function loadFromQuery(Query\Connection $connectionQuery): Connection
$this->relation = $connectionQuery->get('relation');
$this->from = $connectionQuery->get('from');
$this->to = $connectionQuery->get('to');
$this->meta = clone $connectionQuery->get('meta');
$this->meta = new MetaCollection();
$this->meta->fromArray($connectionQuery->get('meta')->toArray());
$this->order = $connectionQuery->get('order');
if ($connectionQuery->get('client')) {
$this->setClient($connectionQuery->get('client'));
}

return $this;
}
Expand Down
5 changes: 1 addition & 4 deletions src/ConnectionCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,13 @@ private function fromArray(array $items, string $collectionType = ''): array
if (! isset($item->to)) {
continue;
}
if (! isset($item->order)) {
continue;
}

$connectionQuery = new Query\Connection();
$connectionQuery
->set('id', $item->ID)
->set('from', $item->from)
->set('to', $item->to)
->set('order', $item->order)
->set('order', $item->order ?? 0)
->set('title', $item->title ?? '')
->set('client', $item->client ?? null)
->set('relation', $item->relation ?? null);
Expand Down
18 changes: 0 additions & 18 deletions src/IQueryTrait.php

This file was deleted.

5 changes: 1 addition & 4 deletions src/Query/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

namespace iTRON\wpConnections\Query;

use iTRON\wpConnections\Abstracts\IQuery;
use iTRON\wpConnections\GSInterface;
use iTRON\wpConnections\IQueryTrait;

class Connection extends \iTRON\wpConnections\Abstracts\Connection implements IQuery
class Connection extends \iTRON\wpConnections\Abstracts\Connection
{
use GSInterface;
use IQueryTrait;

public int $both = 0;

Expand Down
5 changes: 1 addition & 4 deletions src/Query/MetaCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

namespace iTRON\wpConnections\Query;

use iTRON\wpConnections\Abstracts\IQuery;
use iTRON\wpConnections\GSInterface;
use iTRON\wpConnections\IQueryTrait;

class MetaCollection extends \iTRON\wpConnections\MetaCollection implements IQuery
class MetaCollection extends \iTRON\wpConnections\MetaCollection
{
use IQueryTrait;
use GSInterface;

public string $collectionType = Meta::class;
Expand Down
44 changes: 6 additions & 38 deletions src/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,52 +88,20 @@ public function createConnection(Query\Connection $connectionQuery): Connection

$this->getClient()->getStorage()->createConnection($connectionQuery);

return new Connection($connectionQuery);
$connection = new Connection($connectionQuery);
$connection->setClient($this->getClient());

do_action('wpConnections/relation/created', $connection);

return $connection;
}

/**
* @throws ConnectionWrongData
*/
public function updateConnection(Query\Connection $connectionQuery): bool
{
$connectionQuery->set('relation', $this->name);
return $this->getClient()->getStorage()->updateConnection($connectionQuery);
}

/**
* @throws ConnectionWrongData
*/
public function updateConnectionMeta(Query\Connection $connectionQuery): bool
{
$connectionQuery->set('relation', $this->name);
$objectID = $connectionQuery->get('id');

/** @var Query\MetaCollection $metaQuery */
$metaQuery = $connectionQuery->get('meta');

if (! $metaQuery->isUpdate()) {
// Remove all meta fields first if false === isUpdate.
$this->getClient()->getStorage()->removeConnectionMeta($objectID, new Query\MetaCollection());
} else {
// Check the array items for false === $isUpdate fields in order to remove older values.
$toRemove = $metaQuery->where('isUpdate', false);
if (! $toRemove->isEmpty()) {
foreach ($toRemove->getIterator() as $meta) {
/** @var Meta $meta */
$meta->setValue(null);
}
$this->getClient()->getStorage()->removeConnectionMeta($objectID, $toRemove);
}
}

// Finally, insert new meta fields.
if (! $metaQuery->isEmpty()) {
$this->getClient()->getStorage()->addConnectionMeta($objectID, $metaQuery);
}

return true;
}

/**
* @throws ConnectionWrongData
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function setLogging()
};

add_action('wpConnections/storage/findConnections/dbQuery', $f, 10, 2);
add_action('wpConnections/storage/removeConnectionMeta/dbQuery', $f, 10, 2);
add_action('wpConnections/storage/removeConnectionMeta/after', $f, 10, 5);
add_action('wpConnections/storage/deletedSpecificConnections', $f, 10, 3);
}
}
Loading

0 comments on commit 2b7bacc

Please sign in to comment.