Skip to content

Commit

Permalink
feat: resolve PHPStan issues (dvsa/olcs-backend#14)
Browse files Browse the repository at this point in the history
* fix: remove `NamingServiceAwareTraitTest` due to over-coupling

The trait depends on `$this->getRepo()` which doesn't exist in this trait so would require this trait to be used in combination with classes that provide this method.

* fix: resolve various PHPStan issues

* chore: bump `olcs-transfer`

* fix: correctly type the DeclarationTest `ApplicationUndertakings` mock

* fix: assertEmpty on `ConditionsUndertakingsReviewService` instead of `null`

* chore: add `public` and `config` directory to PHPStan scope
  • Loading branch information
JoshuaLicense authored Dec 21, 2023
1 parent 1829782 commit 98a4348
Show file tree
Hide file tree
Showing 72 changed files with 317 additions and 350 deletions.
12 changes: 6 additions & 6 deletions app/api/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 3 additions & 58 deletions app/api/module/Api/src/Domain/AuthAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function getUserRepository()
}

/**
* @return \Dvsa\Olcs\Api\Entity\User\User
* @return \Dvsa\Olcs\Api\Entity\User\User|void
*/
public function getCurrentUser()
{
Expand All @@ -80,7 +80,7 @@ public function getCurrentUser()
/**
* @note Even though this appears to be a one to one relationship, there is only ever one organisation for a user
*
* @return \Dvsa\Olcs\Api\Entity\Organisation\Organisation
* @return \Dvsa\Olcs\Api\Entity\Organisation\Organisation|void
*/
public function getCurrentOrganisation()
{
Expand All @@ -97,7 +97,7 @@ public function getCurrentOrganisation()
* @note Even though this appears to be a one to one relationship, there's only ever one local authority for a user
* olcs-14494 emergency fix, need to clean this up
*
* @return LocalAuthority
* @return LocalAuthority|void
*/
public function getCurrentLocalAuthority()
{
Expand Down Expand Up @@ -131,61 +131,6 @@ public function getUser()
return $this->authService->getIdentity()->getUser();
}

/**
* Note this is only intended for internal users, selfserve users don't have these access permissions
*
* Takes an array of traffic areas that will have come from a transfer object.
* If empty or "all" is selected then return all traffic areas the user has access to
*
* @see TrafficAreas
* @see TrafficAreasOptional
*/
public function modifyTrafficAreaQueryBasedOnUser(QueryInterface $query): QueryInterface
{
$trafficAreas = $query->getTrafficAreas();

if (empty($trafficAreas) || in_array('all', $trafficAreas)) {
/**
* reports have an "other" field which we will need to preserve
* this will be ignored by anything which doesn't support it via an "in" query
*/
$additional = ['other'];

$newData = [
'trafficAreas' => array_merge($this->getInternalUserTrafficAreas(), $additional),
];

$query->exchangeArray($newData);
}

return $query;
}

/**
* get user traffic areas (this data exists for internal users only)
*/
public function getInternalUserTrafficAreas(): array
{
return $this->getUserData()['dataAccess']['trafficAreas'];
}

/**
* gets a copy of the user account data - majority of the time this will come straight from the myaccount cache
* if the cache doesn't exist we'll have a query handler result instead that will need to be serialized
*
* @return array
*/
public function getUserData(): array
{
$accountInfo = $this->getQueryHandler()->handleQuery(MyAccount::create([]));

if ($accountInfo instanceof Result) {
return $accountInfo->serialize();
}

return $accountInfo;
}

/**
* Does the current user have the Internal user role
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@
use Dvsa\Olcs\Api\Domain\TranslatorAwareInterface;
use Dvsa\Olcs\Api\Domain\UploaderAwareInterface;
use Dvsa\Olcs\Api\Domain\Util\SlaCalculatorInterface;
use Dvsa\Olcs\Api\Entity\Application\Application;
use Dvsa\Olcs\Api\Entity\Bus\BusReg;
use Dvsa\Olcs\Api\Entity\Cases\Cases;
use Dvsa\Olcs\Api\Entity\Licence\ContinuationDetail;
use Dvsa\Olcs\Api\Entity\Licence\Licence;
use Dvsa\Olcs\Api\Entity\Organisation\Organisation;
use Dvsa\Olcs\Api\Entity\Permits\IrhpApplication;
use Dvsa\Olcs\Api\Entity\Surrender;
use Dvsa\Olcs\Api\Entity\Tm\TransportManager;
use Dvsa\Olcs\Api\Service\Document\NamingService;
use Dvsa\Olcs\Api\Service\Document\NamingServiceAwareInterface;
use Dvsa\Olcs\Api\Service\Ebsr\TransExchangeClient;
use Dvsa\Olcs\Api\Service\OpenAm\UserInterface;
Expand All @@ -42,6 +52,7 @@
use Dvsa\Olcs\Queue\Service\Queue;
use Dvsa\Olcs\Queue\Service\QueueInterface;
use Dvsa\Olcs\Transfer\Command\CommandInterface;
use Dvsa\Olcs\Transfer\Query\MyAccount\MyAccount;
use Dvsa\Olcs\Transfer\Query\QueryInterface;
use Dvsa\Olcs\Transfer\Service\CacheEncryption as CacheEncryptionService;
use Olcs\Logging\Log\Logger;
Expand Down Expand Up @@ -85,6 +96,11 @@ abstract class AbstractCommandHandler implements CommandHandlerInterface, Factor

private RepositoryServiceManager $repoManager;

/**
* @var NamingService
*/
private $documentNamingService;

/**
* @var IdentityProviderInterface
*/
Expand Down Expand Up @@ -559,4 +575,120 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
}
return $this;
}

/**
* @param NamingService $service
*/
public function setNamingService(NamingService $service)
{
$this->documentNamingService = $service;
}

/**
* @return NamingService
*/
public function getNamingService()
{
return $this->documentNamingService;
}

/**
* @param $command
*/
public function determineEntityFromCommand(array $data)
{
if (!empty($data['case'])) {
return $this->getRepo()->getReference(Cases::class, $data['case']);
}

if (!empty($data['application'])) {
return $this->getRepo()->getReference(Application::class, $data['application']);
}

if (!empty($data['transportManager'])) {
return $this->getRepo()->getReference(TransportManager::class, $data['transportManager']);
}

if (!empty($data['busReg'])) {
return $this->getRepo()->getReference(BusReg::class, $data['busReg']);
}

if (!empty($data['licence'])) {
return $this->getRepo()->getReference(Licence::class, $data['licence']);
}

if (!empty($data['irfoOrganisation'])) {
return $this->getRepo()->getReference(Organisation::class, $data['irfoOrganisation']);
}

if (!empty($data['continuationDetail'])) {
return $this->getRepo()->getReference(ContinuationDetail::class, $data['continuationDetail']);
}

if (!empty($data['surrender'])) {
return $this->getRepo()->getReference(Surrender::class, $data['surrender']);
}

if (!empty($data['irhpApplication'])) {
return $this->getRepo()->getReference(IrhpApplication::class, $data['irhpApplication']);
}

return null;
}


/**
* Note this is only intended for internal users, selfserve users don't have these access permissions
*
* Takes an array of traffic areas that will have come from a transfer object.
* If empty or "all" is selected then return all traffic areas the user has access to
*
* @see TrafficAreas
* @see TrafficAreasOptional
*/
public function modifyTrafficAreaQueryBasedOnUser(QueryInterface $query): QueryInterface
{
$trafficAreas = $query->getTrafficAreas();

if (empty($trafficAreas) || in_array('all', $trafficAreas)) {
/**
* reports have an "other" field which we will need to preserve
* this will be ignored by anything which doesn't support it via an "in" query
*/
$additional = ['other'];

$newData = [
'trafficAreas' => array_merge($this->getInternalUserTrafficAreas(), $additional),
];

$query->exchangeArray($newData);
}

return $query;
}

/**
* get user traffic areas (this data exists for internal users only)
*/
public function getInternalUserTrafficAreas(): array
{
return $this->getUserData()['dataAccess']['trafficAreas'];
}

/**
* gets a copy of the user account data - majority of the time this will come straight from the myaccount cache
* if the cache doesn't exist we'll have a query handler result instead that will need to be serialized
*
* @return array
*/
public function getUserData(): array
{
$accountInfo = $this->getQueryHandler()->handleQuery(MyAccount::create([]));

if ($accountInfo instanceof \Dvsa\Olcs\Api\Domain\QueryHandler\Result) {
return $accountInfo->serialize();
}

return $accountInfo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Dvsa\Olcs\Api\Domain\Command\Application\UpdateApplicationCompletion as UpdateApplicationCompletionCmd;
use Dvsa\Olcs\Api\Domain\CommandHandler\AbstractCommandHandler;
use Dvsa\Olcs\Api\Domain\CommandHandler\TransactionedInterface;
use Dvsa\Olcs\Api\Domain\Service\OperatingCentreHelper;
use Dvsa\Olcs\Api\Entity\Application\ApplicationOperatingCentre;
use Dvsa\Olcs\Api\Entity\OperatingCentre\OperatingCentre;
use Dvsa\Olcs\Transfer\Command\CommandInterface;
Expand Down Expand Up @@ -34,6 +35,8 @@ final class CreateOperatingCentre extends AbstractCommandHandler implements Tran
'ApplicationOperatingCentre'
];

private OperatingCentreHelper $helper;

/**
* @param Cmd $command
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Dvsa\Olcs\Api\Domain\CommandHandler\AbstractCommandHandler;
use Dvsa\Olcs\Api\Domain\Exception\ValidationException;
use Dvsa\Olcs\Api\Entity\System\Category;
use Dvsa\Olcs\Snapshot\Service\Snapshots\ApplicationReview\GeneratorFactory;
use Dvsa\Olcs\Transfer\Command\CommandInterface;
use Dvsa\Olcs\Api\Entity\Application\Application as ApplicationEntity;
use Dvsa\Olcs\Transfer\Command\Document\Upload;
Expand All @@ -39,6 +40,11 @@ final class CreateSnapshot extends AbstractCommandHandler implements AuthAwareIn

protected $repoServiceName = 'Application';

/**
* @var GeneratorFactory
*/
private $reviewSnapshotService;

public function handleCommand(CommandInterface $command)
{
/** @var ApplicationEntity $application */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Dvsa\Olcs\Api\Entity\System\SubCategory;
use Dvsa\Olcs\Api\Entity\User\User;
use Dvsa\Olcs\Api\Service\Document\NamingServiceAwareInterface;
use Dvsa\Olcs\Api\Service\Document\NamingServiceAwareTrait;
use Dvsa\Olcs\Transfer\Command as TransferCmd;
use Dvsa\Olcs\Transfer\Command\CommandInterface;
use Dvsa\Olcs\Api\Domain\Exception\RuntimeException;
Expand All @@ -29,11 +28,9 @@
class Create extends AbstractCommandHandler implements
TransactionedInterface,
UploaderAwareInterface,
NamingServiceAwareInterface,
AuthAwareInterface
{
use UploaderAwareTrait;
use NamingServiceAwareTrait;
use AuthAwareTrait;
use DocTemplateTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Dvsa\Olcs\Api\Entity\System\Category;
use Dvsa\Olcs\Api\Entity\System\SubCategory;
use Dvsa\Olcs\Api\Service\Document\NamingServiceAwareInterface;
use Dvsa\Olcs\Api\Service\Document\NamingServiceAwareTrait;
use Dvsa\Olcs\Transfer\Command as TransferCmd;
use Dvsa\Olcs\Transfer\Command\CommandInterface;

Expand All @@ -32,7 +31,6 @@ class Update extends AbstractCommandHandler implements
AuthAwareInterface
{
use UploaderAwareTrait;
use NamingServiceAwareTrait;
use AuthAwareTrait;
use DocTemplateTrait;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Dvsa\Olcs\Api\Domain\DocumentGeneratorAwareTrait;
use Dvsa\Olcs\Api\Entity\System\Category;
use Dvsa\Olcs\Api\Service\Document\NamingServiceAwareInterface;
use Dvsa\Olcs\Api\Service\Document\NamingServiceAwareTrait;
use Dvsa\Olcs\Transfer\Command\CommandInterface;
use Dvsa\Olcs\Api\Domain\Command\Document\GenerateAndStore as Cmd;

Expand All @@ -36,7 +35,6 @@ final class GenerateAndStore extends AbstractCommandHandler implements
{
use DocumentGeneratorAwareTrait;
use AuthAwareTrait;
use NamingServiceAwareTrait;

protected $repoServiceName = 'Document';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Dvsa\Olcs\Api\Domain\UploaderAwareInterface;
use Dvsa\Olcs\Api\Domain\UploaderAwareTrait;
use Dvsa\Olcs\Api\Service\Document\NamingServiceAwareInterface;
use Dvsa\Olcs\Api\Service\Document\NamingServiceAwareTrait;
use Dvsa\Olcs\Api\Service\File\MimeNotAllowedException;
use Dvsa\Olcs\DocumentShare\Data\Object\File as DsFile;
use Dvsa\Olcs\Transfer\Command as TransferCmd;
Expand All @@ -28,7 +27,6 @@ final class Upload extends AbstractCommandHandler implements
NamingServiceAwareInterface
{
use UploaderAwareTrait;
use NamingServiceAwareTrait;

public const ERR_MIME = 'ERR_MIME';
public const ERR_EBSR_MIME = 'ERR_EBSR_MIME';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Dvsa\Olcs\Api\Domain\CommandHandler;

use Dvsa\Olcs\Api\Domain\Command\Result;
use Dvsa\Olcs\Api\Domain\EmailAwareInterface;
use Dvsa\Olcs\Api\Domain\EmailAwareTrait;
use Dvsa\Olcs\Transfer\Command\CommandInterface;
Expand All @@ -17,5 +18,6 @@ class EmailAwareTraitTestStub extends AbstractCommandHandler implements EmailAwa

public function handleCommand(CommandInterface $command)
{
return new Result();
}
}
Loading

0 comments on commit 98a4348

Please sign in to comment.