Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
hokoo committed Oct 20, 2024
1 parent 672c44e commit 76c293e
Show file tree
Hide file tree
Showing 16 changed files with 289 additions and 165 deletions.
5 changes: 5 additions & 0 deletions .run/WPC remote debug.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="WPC remote debug" type="PhpRemoteDebugRunConfigurationType" factoryName="PHP Remote Debug" singleton="false" filter_connections="FILTER" server_name="wpc localhost" session_id="PHPSTORM">
<method v="2" />
</configuration>
</component>
3 changes: 2 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ tests.docker.connect:

tests.run:
cd ./tests/dev/ && \
docker-compose -p wpc-tests exec php sh -c 'vendor/bin/phpunit -c phpunit.xml && vendor/bin/phpunit -c php-wp-unit.xml'
docker-compose -p wpc-tests exec php sh -c 'vendor/bin/phpunit -c php-wp-unit.xml'
#docker-compose -p wpc-tests exec php sh -c 'vendor/bin/phpunit -c phpunit.xml && vendor/bin/phpunit -c php-wp-unit.xml'
13 changes: 9 additions & 4 deletions php-wp-unit.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
<?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"
convertNoticesToExceptions="false"
convertWarningsToExceptions="false"
verbose="true"
>
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<php>
<const name="DOING_TESTS" value="1" />
<const name="CLIENT_NAME" value="client-test" />
<const name="RELATION_NAME" value="relation-test" />
</php>
<coverage includeUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="wpConnections creating for WordPress test suite">
<directory suffix=".php">tests/iTRON/wpConnections/WP</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.

21 changes: 10 additions & 11 deletions src/Abstracts/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@

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;
/**
*
* @return bool Successful or not.
*/
abstract public function updateConnection(Connection $connection): bool;

/**
* Deletes connections by set of connection IDs.
Expand Down Expand Up @@ -64,11 +62,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
33 changes: 20 additions & 13 deletions src/ClientRestApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,31 +100,38 @@ 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'));
$queryConnection = new Query\Connection();
$queryConnection->id = $request->get_param('connectionID');
$found = $this->getClient()->getRelation( $request->get_param( 'relation' ) )->findConnections( $queryConnection );

if ( $found->isEmpty() ) {
return rest_ensure_response( $this->getError( new ConnectionNotFound( new Exception( serialize( $request->get_params() ) ) ) ) );
}

$connection = $found->first();

if ('PUT' === $request->get_method()) {
$queryConnection->meta->setIsUpdate(false);
$connection->meta->clear();
}

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

if ('PATCH' === $request->get_method()) {
$queryConnection->meta->map(
function ($item) {
/** @var Query\Connection $item */
$item->setIsUpdate(false);
}
);
$filtered_meta = $connection->meta->filter(function ($key) use ($request) {
return ! in_array($key, array_column($request->get_param('meta'), 'key'));
});

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

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

try {
$result = $this->getClient()->getRelation($request->get_param('relation'))->updateConnectionMeta($queryConnection);
$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
29 changes: 23 additions & 6 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 @@ -18,11 +20,22 @@ public function __clone()
$this->meta = clone $this->meta;
}

/**
* Saves instance to DB
*/
public function save()
{
/**
* 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 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 +52,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
2 changes: 1 addition & 1 deletion src/Exceptions/ConnectionNotFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class ConnectionNotFound extends Exception
{
public function __construct(Throwable $previous = null)
{
parent::__construct('Connection not found.', 2, $previous);
parent::__construct('Connection not found.' . $previous->getMessage(), 2, $previous);
}
}
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 76c293e

Please sign in to comment.