mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-03 06:54:34 +02:00
Add a reference to the used info provider to a part
This commit is contained in:
parent
db97114fb4
commit
a95ba1acc4
11 changed files with 481 additions and 0 deletions
67
src/Entity/OAuthToken.php
Normal file
67
src/Entity/OAuthToken.php
Normal file
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Entity\Base\AbstractDBElement;
|
||||
use App\Entity\Base\AbstractNamedDBElement;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
/**
|
||||
* This entity represents a OAuth token pair (access and refresh token), for an application
|
||||
*/
|
||||
#[ORM\Entity()]
|
||||
#[ORM\Table(name: 'oauth_tokens')]
|
||||
#[ORM\UniqueConstraint(name: 'oauth_tokens_unique_name', columns: ['name'])]
|
||||
#[ORM\Index(columns: ['name'], name: 'oauth_tokens_name_idx')]
|
||||
class OAuthToken extends AbstractNamedDBElement
|
||||
{
|
||||
/** @var string|null The short-term usable OAuth2 token */
|
||||
#[ORM\Column(type: 'string', nullable: true)]
|
||||
private ?string $token = null;
|
||||
|
||||
/** @var \DateTimeInterface The date when the token expires */
|
||||
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
|
||||
private ?\DateTimeInterface $expires_at = null;
|
||||
|
||||
/** @var string The refresh token for the OAuth2 auth */
|
||||
#[ORM\Column(type: 'string')]
|
||||
private string $refresh_token = '';
|
||||
|
||||
public function __construct(string $name, string $refresh_token, string $token = null, \DateTimeInterface $expires_at = null)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
//If token is given, you also have to give the expires_at date
|
||||
if ($token !== null && $expires_at === null) {
|
||||
throw new \InvalidArgumentException('If you give a token, you also have to give the expires_at date');
|
||||
}
|
||||
|
||||
$this->name = $name;
|
||||
$this->refresh_token = $refresh_token;
|
||||
$this->expires_at = $expires_at;
|
||||
$this->token = $token;
|
||||
}
|
||||
|
||||
}
|
155
src/Entity/Parts/InfoProviderReference.php
Normal file
155
src/Entity/Parts/InfoProviderReference.php
Normal file
|
@ -0,0 +1,155 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace App\Entity\Parts;
|
||||
|
||||
use App\Services\InfoProviderSystem\DTOs\SearchResultDTO;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping\Column;
|
||||
use Doctrine\ORM\Mapping\Embeddable;
|
||||
|
||||
/**
|
||||
* This class represents a reference to a info provider inside a part.
|
||||
*/
|
||||
#[Embeddable]
|
||||
class InfoProviderReference
|
||||
{
|
||||
|
||||
/** @var string|null The key referencing the provider used to get this part, or null if it was not provided by a data provider */
|
||||
#[Column(type: 'string', nullable: true)]
|
||||
private ?string $provider_key = null;
|
||||
|
||||
/** @var string|null The id of this part inside the provider system or null if the part was not provided by a data provider */
|
||||
#[Column(type: 'string', nullable: true)]
|
||||
private ?string $provider_id = null;
|
||||
|
||||
/**
|
||||
* @var string|null The url of this part inside the provider system or null if this info is not existing
|
||||
*/
|
||||
#[Column(type: 'string', nullable: true)]
|
||||
private ?string $provider_url = null;
|
||||
|
||||
#[Column(type: Types::DATETIME_MUTABLE, options: ['default' => 'CURRENT_TIMESTAMP'])]
|
||||
private ?\DateTimeInterface $last_updated = null;
|
||||
|
||||
/**
|
||||
* Constructing is forbidden from outside.
|
||||
*/
|
||||
private function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the key usable to identify the provider, which provided this part. Returns null, if the part was not created by a provider.
|
||||
* @return string|null
|
||||
*/
|
||||
public function getProviderKey(): ?string
|
||||
{
|
||||
return $this->provider_key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the id of this part inside the provider system or null if the part was not provided by a data provider.
|
||||
* @return string|null
|
||||
*/
|
||||
public function getProviderId(): ?string
|
||||
{
|
||||
return $this->provider_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the url of this part inside the provider system or null if this info is not existing.
|
||||
* @return string|null
|
||||
*/
|
||||
public function getProviderUrl(): ?string
|
||||
{
|
||||
return $this->provider_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the time, when the part was last time updated by the provider.
|
||||
* @return \DateTimeInterface|null
|
||||
*/
|
||||
public function getLastUpdated(): ?\DateTimeInterface
|
||||
{
|
||||
return $this->last_updated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true, if this part was created based on infos from a provider.
|
||||
* Or false, if this part was created by a user manually.
|
||||
* @return bool
|
||||
*/
|
||||
public function isProviderCreated(): bool
|
||||
{
|
||||
return $this->provider_key !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance, without any provider information.
|
||||
* Use this for parts, which are created by a user manually.
|
||||
* @return InfoProviderReference
|
||||
*/
|
||||
public static function noProvider(): self
|
||||
{
|
||||
$ref = new InfoProviderReference();
|
||||
$ref->provider_key = null;
|
||||
$ref->provider_id = null;
|
||||
$ref->provider_url = null;
|
||||
$ref->last_updated = null;
|
||||
return $ref;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a reference to an info provider based on the given parameters.
|
||||
* @param string $provider_key
|
||||
* @param string $provider_id
|
||||
* @param string|null $provider_url
|
||||
* @return self
|
||||
*/
|
||||
public static function providerReference(string $provider_key, string $provider_id, ?string $provider_url = null): self
|
||||
{
|
||||
$ref = new InfoProviderReference();
|
||||
$ref->provider_key = $provider_key;
|
||||
$ref->provider_id = $provider_id;
|
||||
$ref->provider_url = $provider_url;
|
||||
$ref->last_updated = new \DateTimeImmutable();
|
||||
return $ref;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a reference to an info provider based on the given Part DTO
|
||||
* @param SearchResultDTO $dto
|
||||
* @return self
|
||||
*/
|
||||
public static function fromPartDTO(SearchResultDTO $dto): self
|
||||
{
|
||||
$ref = new InfoProviderReference();
|
||||
$ref->provider_key = $dto->provider_key;
|
||||
$ref->provider_id = $dto->provider_id;
|
||||
$ref->provider_url = $dto->provider_url;
|
||||
$ref->last_updated = new \DateTimeImmutable();
|
||||
return $ref;
|
||||
}
|
||||
}
|
|
@ -114,6 +114,9 @@ class Part extends AttachmentContainingDBElement
|
|||
$this->orderdetails = new ArrayCollection();
|
||||
$this->parameters = new ArrayCollection();
|
||||
$this->project_bom_entries = new ArrayCollection();
|
||||
|
||||
//By default, the part has no provider
|
||||
$this->providerReference = InfoProviderReference::noProvider();
|
||||
}
|
||||
|
||||
public function __clone()
|
||||
|
@ -139,6 +142,9 @@ class Part extends AttachmentContainingDBElement
|
|||
foreach ($parameters as $parameter) {
|
||||
$this->addParameter(clone $parameter);
|
||||
}
|
||||
|
||||
//Deep clone info provider
|
||||
$this->providerReference = clone $this->providerReference;
|
||||
}
|
||||
parent::__clone();
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Entity\Parts\PartTraits;
|
||||
|
||||
use App\Entity\Parts\InfoProviderReference;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use App\Entity\Parts\Part;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
@ -63,6 +64,12 @@ trait AdvancedPropertyTrait
|
|||
#[ORM\Column(type: Types::STRING, length: 100, nullable: true, unique: true)]
|
||||
protected ?string $ipn = null;
|
||||
|
||||
/**
|
||||
* @var InfoProviderReference The reference to the info provider, that provided the information about this part
|
||||
*/
|
||||
#[ORM\Embedded(class: InfoProviderReference::class, columnPrefix: 'provider_reference_')]
|
||||
protected InfoProviderReference $providerReference;
|
||||
|
||||
/**
|
||||
* Checks if this part is marked, for that it needs further review.
|
||||
*/
|
||||
|
@ -150,5 +157,27 @@ trait AdvancedPropertyTrait
|
|||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the reference to the info provider, that provided the information about this part.
|
||||
* @return InfoProviderReference
|
||||
*/
|
||||
public function getProviderReference(): InfoProviderReference
|
||||
{
|
||||
return $this->providerReference;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the reference to the info provider, that provided the information about this part.
|
||||
* @param InfoProviderReference $providerReference
|
||||
* @return AdvancedPropertyTrait
|
||||
*/
|
||||
public function setProviderReference(InfoProviderReference $providerReference): self
|
||||
{
|
||||
$this->providerReference = $providerReference;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue