Skip to content

Commit

Permalink
Refactor all utils classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jkniest committed Jun 3, 2024
1 parent 8dd4d83 commit 2d1af0d
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 86 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- CategoryUtils
- Removed method `getFirst` on CategoryUtils
- Removed method `getByName` on CategoryUtils
- Renamed `CategoryUtils` to `SalutationUtils`

## [2.4.0] - 2023-11-15
### Added
Expand Down
4 changes: 2 additions & 2 deletions _examples/CustomerFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function load(FixtureBag $bag): void
'defaultPaymentMethodId' => $this->helper->PaymentMethod()->getInvoicePaymentMethod()->getId(),
'defaultBillingAddress' => [
'id' => self::ADDRESS_ID,
'salutationId' => $this->helper->Customer()->getNotSpecifiedSalutation()->getId(),
'salutationId' => $this->helper->Salutation()->getNotSpecifiedSalutation()->getId(),
'firstName' => 'John',
'lastName' => 'Doe',
'zipcode' => '1234',
Expand All @@ -41,7 +41,7 @@ public function load(FixtureBag $bag): void
'countryId' => $this->helper->SalesChannel()->getCountry('DE')->getId(),
],
'defaultShippingAddressId' => self::ADDRESS_ID,
'salutationId' => $this->helper->Customer()->getNotSpecifiedSalutation()->getId(),
'salutationId' => $this->helper->Salutation()->getNotSpecifiedSalutation()->getId(),
'customerNumber' => '1122',
'firstName' => 'John',
'lastName' => 'Doe',
Expand Down
10 changes: 5 additions & 5 deletions src/FixtureHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

use Basecom\FixturePlugin\Utils\CategoryUtils;
use Basecom\FixturePlugin\Utils\CmsUtils;
use Basecom\FixturePlugin\Utils\CustomerUtils;
use Basecom\FixturePlugin\Utils\DatabaseUtils;
use Basecom\FixturePlugin\Utils\MediaUtils;
use Basecom\FixturePlugin\Utils\PaymentMethodUtils;
use Basecom\FixturePlugin\Utils\SalesChannelUtils;
use Basecom\FixturePlugin\Utils\SalutationUtils;
use Basecom\FixturePlugin\Utils\ShippingMethodUtils;

readonly class FixtureHelper
Expand All @@ -22,7 +22,7 @@ public function __construct(
private CmsUtils $cmsUtils,
private PaymentMethodUtils $paymentMethodUtils,
private ShippingMethodUtils $shippingMethodUtils,
private CustomerUtils $customerUtils,
private SalutationUtils $salutationUtils,
private DatabaseUtils $databaseUtils,
) {
}
Expand Down Expand Up @@ -55,12 +55,12 @@ public function SalesChannel(): SalesChannelUtils
}

/**
* Use this to access the customer related features
* Use this to access the salutation related features
* of the fixture helper class.
*/
public function Customer(): CustomerUtils
public function Salutation(): SalutationUtils
{
return $this->customerUtils;
return $this->salutationUtils;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/CategoryUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(
*/
public function getRootCategory(): ?CategoryEntity
{
return once(function () {
return once(function (): ?CategoryEntity {
$criteria = (new Criteria())
->addFilter(new EqualsFilter('autoIncrement', 1))
->addFilter(new EqualsFilter('level', 1))
Expand Down
29 changes: 21 additions & 8 deletions src/Utils/CmsUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;

/**
* This class provides utility methods to work with the shopware cms. It has build in caching to prevent
* multiple database queries for the same data within one command execution / request.
*
* This class is designed to be used through the FixtureHelper, using:
* ```php
* $this->helper->Cms()->……();
* ```
*/
readonly class CmsUtils
{
/**
Expand All @@ -24,15 +33,19 @@ public function __construct(

public function getDefaultCategoryLayout(): ?CmsPageEntity
{
$criteria = (new Criteria())
->addFilter(new EqualsFilter('locked', '1'))
->addFilter(new EqualsAnyFilter('translations.name', ['Default category layout', 'Default listing layout']))
->setLimit(1);
return once(function (): ?CmsPageEntity {
$criteria = (new Criteria())
->addFilter(new EqualsFilter('locked', '1'))
->addFilter(new EqualsAnyFilter('translations.name', ['Default category layout', 'Default listing layout']))
->setLimit(1);

$cmsPage = $this->cmsPageRepository
->search($criteria, Context::createDefaultContext())
->first();
$criteria->setTitle(sprintf('%s::%s()', __CLASS__, __FUNCTION__));

return $cmsPage instanceof CmsPageEntity ? $cmsPage : null;
$cmsPage = $this->cmsPageRepository
->search($criteria, Context::createDefaultContext())
->first();

return $cmsPage instanceof CmsPageEntity ? $cmsPage : null;
});
}
}
36 changes: 0 additions & 36 deletions src/Utils/CustomerUtils.php

This file was deleted.

10 changes: 10 additions & 0 deletions src/Utils/DatabaseUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;

/**
* This class provides utility methods to directly interact with the database. It has build in
* caching to prevent multiple database queries for the same data within one command
* execution / request.
*
* This class is designed to be used through the FixtureHelper, using:
* ```php
* $this->helper->Database()->……();
* ```
*/
readonly class DatabaseUtils
{
public function __construct(
Expand Down
43 changes: 31 additions & 12 deletions src/Utils/MediaUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;

/**
* This class provides utility methods to work with media assets. It has build in caching to prevent
* multiple database queries for the same data within one command execution / request.
*
* This class is designed to be used through the FixtureHelper, using:
* ```php
* $this->helper->Media()->……();
* ```
*/
readonly class MediaUtils
{
/**
Expand All @@ -30,31 +39,41 @@ public function __construct(

/**
* Copied from "vendor/shopware/core/Content/Media/MediaService.php".
*
* Extended it to include simple cache
*/
public function getDefaultFolder(string $folderName): ?MediaFolderEntity
{
$criteria = (new Criteria())
->addFilter(new EqualsFilter('media_folder.defaultFolder.entity', $folderName))
->addAssociation('defaultFolder')
->setLimit(1);
return once(function () use ($folderName): ?MediaFolderEntity {
$criteria = (new Criteria())
->addFilter(new EqualsFilter('media_folder.defaultFolder.entity', $folderName))
->addAssociation('defaultFolder')
->setLimit(1);

$mediaFolder = $this->mediaFolderRepository
->search($criteria, Context::createDefaultContext())
->first();
$criteria->setTitle(sprintf('%s::%s()', __CLASS__, __FUNCTION__));

return $mediaFolder instanceof MediaFolderEntity ? $mediaFolder : null;
$mediaFolder = $this->mediaFolderRepository
->search($criteria, Context::createDefaultContext())
->first();

return $mediaFolder instanceof MediaFolderEntity ? $mediaFolder : null;
});
}

/**
* "Upload" a file within shopware. It takes a real file path ($filename) and uploads it as a full media.
*/
public function upload(string $mediaId, string $folderId, string $filename, string $extension, string $contentType): void
{
$ctx = Context::createDefaultContext();

$this->mediaRepository->upsert([
$this->mediaRepository->upsert(
[
'id' => $mediaId,
'mediaFolderId' => $folderId,
[
'id' => $mediaId,
'mediaFolderId' => $folderId,
],
],
],
$ctx,
);

Expand Down
30 changes: 23 additions & 7 deletions src/Utils/PaymentMethodUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;

/**
* This class provides utility methods to work with payment methods. It has build in caching to prevent
* multiple database queries for the same data within one command execution / request.
*
* This class is designed to be used through the FixtureHelper, using:
* ```php
* $this->helper->PaymentMethod()->……();
* ```
*/
readonly class PaymentMethodUtils
{
/**
Expand All @@ -22,16 +31,23 @@ public function __construct(
) {
}

/**
* Return the default invoice payment method of shopware.
*/
public function getInvoicePaymentMethod(): ?PaymentMethodEntity
{
$criteria = (new Criteria())->addFilter(
new EqualsFilter('handlerIdentifier', InvoicePayment::class),
)->setLimit(1);
return once(function (): ?PaymentMethodEntity {
$criteria = (new Criteria())->addFilter(
new EqualsFilter('handlerIdentifier', InvoicePayment::class),
)->setLimit(1);

$criteria->setTitle(sprintf('%s::%s()', __CLASS__, __FUNCTION__));

$paymentMethod = $this->paymentMethodRepository
->search($criteria, Context::createDefaultContext())
->first();
$paymentMethod = $this->paymentMethodRepository
->search($criteria, Context::createDefaultContext())
->first();

return $paymentMethod instanceof PaymentMethodEntity ? $paymentMethod : null;
return $paymentMethod instanceof PaymentMethodEntity ? $paymentMethod : null;
});
}
}
Loading

0 comments on commit 2d1af0d

Please sign in to comment.