Skip to content

[OPSRC-536] Add Image property to ShippingMethod #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion behat.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ default:
screenshot: true

Behat\MinkExtension:
files_path: "%paths.base%/vendor/sylius/sylius/src/Sylius/Behat/Resources/fixtures/"
files_path: "%paths.base%/tests/Application/Behat/Resources/private/"
base_url: "https://127.0.0.1:8080/"
default_session: symfony
javascript_session: chrome_headless
Expand Down
36 changes: 35 additions & 1 deletion doc/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ This plugin was made on top
of [SyliusShippingExportPlugin](https://github.com/BitBagCommerce/SyliusShippingExportPlugin), so please remember to do
the same for it's configuration.

Add trait and interface to your Order entity class:
Add trait and interface to your Order and ShippingMethod entity classes:

```php
<?php
Expand All @@ -59,6 +59,23 @@ class Order extends BaseOrder implements InPostPointsAwareInterface
}
```

```php
<?php

declare(strict_types=1);

namespace App\Entity\Order;

use BitBag\SyliusInPostPlugin\Model\ShippingMethodImageTrait;
use Sylius\Component\Core\Model\ImageAwareInterface;
use Sylius\Component\Core\Model\ShippingMethod as BaseShippingMethod;

class ShippingMethod extends BaseShippingMethod implements ImageAwareInterface
{
use ShippingMethodImageTrait;
}
```

Define new Entity mapping inside your src/Resources/config/doctrine directory. (You can do it with annotations if you
prefer)

Expand All @@ -85,7 +102,23 @@ prefer)
</entity>
</doctrine-mapping>
```
```xml
<?xml version="1.0" encoding="UTF-8"?>

<doctrine-mapping
xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"
>
<entity name="Tests\BitBag\SyliusInPostPlugin\Application\src\Entity\ShippingMethodImage" table="bitbag_inpost_shipping_method_image">
<one-to-one field="owner" target-entity="Sylius\Component\Shipping\Model\ShippingMethodInterface" inversed-by="image">
<join-column name="owner_id" referenced-column-name="id" nullable="false" on-delete="CASCADE"/>
</one-to-one>
</entity>

</doctrine-mapping>
```
Finish the installation by updating the database schema:

```
Expand All @@ -103,6 +136,7 @@ $ cd tests/Application
$ yarn install
$ yarn encore dev
$ bin/console assets:install public -e test
$ bin/console doctrine:database:create -e test
$ bin/console doctrine:schema:create -e test
$ bin/console server:run 127.0.0.1:8080 -d public -e test
$ open http://localhost:8080
Expand Down
17 changes: 17 additions & 0 deletions features/adding_image_to_shipping_method.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@managing_shipping_image_inpost
Feature: Adding shipping method image
In order to display shipping method image during shipment selection
As an Administrator
I want to be able to add image to shipping method

Background:
Given the store operates on a single channel in the "United States" named "Web-US"
And I am logged in as an administrator
And the store has "Inpost" shipping method with "$10.00" fee

@ui
Scenario: Seeing shipments to export
When I want to modify a shipping method "InPost"
And I upload the "image/shipping_logo.jpg" image as shipping method logo
And I save my changes
Then I should be notified that it has been successfully edited
18 changes: 18 additions & 0 deletions src/Entity/ShippingMethodImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace BitBag\SyliusInPostPlugin\Entity;

use Sylius\Component\Core\Model\Image;

class ShippingMethodImage extends Image implements ShippingMethodImageInterface
{

}
18 changes: 18 additions & 0 deletions src/Entity/ShippingMethodImageInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace BitBag\SyliusInPostPlugin\Entity;

use Sylius\Component\Core\Model\ImageInterface;

interface ShippingMethodImageInterface extends ImageInterface
{

}
50 changes: 50 additions & 0 deletions src/EventListener/ImageUploadListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace BitBag\SyliusInPostPlugin\EventListener;

use Sylius\Component\Core\Model\ImageAwareInterface;
use Sylius\Component\Core\Model\ImageInterface;
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Webmozart\Assert\Assert;

final class ImageUploadListener
{
private ImageUploaderInterface $uploader;

public function __construct(ImageUploaderInterface $uploader)
{
$this->uploader = $uploader;
}

public function uploadImages(GenericEvent $event): void
{
$subject = $event->getSubject();

Assert::isInstanceOf($subject, ImageAwareInterface::class);

if (empty($subject->getImage()) || !$subject->getImage()->hasFile()) {
return;
}

/** @var ImageInterface $image */
$image = $subject->getImage();

/** @var UploadedFile $file */
$file = $image->getFile();

$this->uploader->upload($image);

$image->setType($file->getMimeType());
$image->setOwner($subject);
}
}
36 changes: 36 additions & 0 deletions src/Form/Type/Extension/ShippingMethodTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace BitBag\SyliusInPostPlugin\Form\Type\Extension;

use BitBag\SyliusInPostPlugin\Form\Type\ShippingMethodImageType;
use Sylius\Bundle\ShippingBundle\Form\Type\ShippingMethodType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;

final class ShippingMethodTypeExtension extends AbstractTypeExtension
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('image', ShippingMethodImageType::class, [
'label' => false,
'required' => false,
])
;
}

public static function getExtendedTypes(): iterable
{
return [
ShippingMethodType::class
];
}
}
34 changes: 34 additions & 0 deletions src/Form/Type/ShippingMethodImageType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace BitBag\SyliusInPostPlugin\Form\Type;

use Sylius\Bundle\CoreBundle\Form\Type\ImageType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormBuilderInterface;

class ShippingMethodImageType extends ImageType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->remove('type')
->add('file', FileType::class, [
'label' => 'sylius.form.image.file',
])
;
}


public function getBlockPrefix(): string
{
return 'bitbag_inpost_shipping_method_image';
}
}
28 changes: 28 additions & 0 deletions src/Model/ShippingMethodImageTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace BitBag\SyliusInPostPlugin\Model;

use Sylius\Component\Core\Model\ImageInterface;

trait ShippingMethodImageTrait
{
protected ?ImageInterface $image;

public function getImage(): ?ImageInterface
{
return $this->image;
}

public function setImage(?ImageInterface $image): void
{
$this->image = $image;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.shipping-method-image{
width: 50px;
}

#shipping-method-label{
display: flex;
gap: 0.5em;
align-items: center;

.header{
color: black;
font-weight: bold;
}

.field{
margin: 0;
}
}
1 change: 1 addition & 0 deletions src/Resources/assets/common/scss/components/main.scss
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
@import "./inpost-point";
@import "./map-widget";
@import "./shipping-method-image";
11 changes: 11 additions & 0 deletions src/Resources/config/doctrine/ShippingMethodImage.orm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:gedmo="http://gediminasm.org/schemas/orm/doctrine-extensions-mapping">

<mapped-superclass name="BitBag\SyliusInPostPlugin\Entity\ShippingMethodImage" table="bitbag_inpost_shipping_method_image">
<one-to-one field="owner" target-entity="Sylius\Component\Shipping\Model\ShippingMethodInterface" inversed-by="image">
<join-column name="owner_id" referenced-column-name="id" nullable="false" on-delete="CASCADE"/>
</one-to-one>
</mapped-superclass>

</doctrine-mapping>
5 changes: 5 additions & 0 deletions src/Resources/config/resource/bitbag_inpost.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ sylius_resource:
classes:
model: BitBag\SyliusInPostPlugin\Entity\InPostPoint
interface: BitBag\SyliusInPostPlugin\Entity\InPostPointInterface
bitbag.shipping_method_image:
driver: doctrine/orm
classes:
model: BitBag\SyliusInPostPlugin\Entity\ShippingMethodImage
form: BitBag\SyliusInPostPlugin\Form\Type\ShippingMethodImageType
9 changes: 9 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@
<tag name="kernel.event_listener" event="bitbag.shipping_export.export_shipment" method="exportShipment"/>
</service>

<service
class="BitBag\SyliusInPostPlugin\EventListener\ImageUploadListener"
id="bitbag.sylius_inpost_plugin.event_listener.image_upload_listener"
>
<argument type="service" id="sylius.image_uploader" />
<tag name="kernel.event_listener" event="sylius.shipping_method.pre_create" method="uploadImages" />
<tag name="kernel.event_listener" event="sylius.shipping_method.pre_update" method="uploadImages" />
</service>

<service
class="BitBag\SyliusInPostPlugin\Api\WebClient"
id="bitbag.sylius_inpost_plugin.api.web_client"
Expand Down
10 changes: 10 additions & 0 deletions src/Resources/config/services/form.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="BitBag\SyliusInPostPlugin\Form\Type\ShippingMethodImageType">
<argument>%bitbag.model.shipping_method_image.class%</argument>
<tag name="form.type" />
</service>
</services>
</container>
7 changes: 7 additions & 0 deletions src/Resources/config/services/form_extension.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,12 @@
<argument>%sylius.form.type.checkout_select_payment.validation_groups%</argument>
<tag name="form.type_extension" />
</service>

<service
class="BitBag\SyliusInPostPlugin\Form\Type\Extension\ShippingMethodTypeExtension"
id="bitbag.sylius_inpost_plugin.form.type.extension.shipping_method_type_extension"
>
<tag name="form.type_extension" extended-type="Sylius\Bundle\ShippingBundle\Form\Type\ShippingMethodType" />
</service>
</services>
</container>
1 change: 1 addition & 0 deletions src/Resources/translations/messages.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ bitbag_sylius_inpost_plugin:
sandbox: Sandbox
production: Production
service: Service
file: File
insurance_amount: Insurance amount
cod_payment_method_code: COD Payment Method Code
additional_services: Additional Services
Expand Down
6 changes: 6 additions & 0 deletions src/Resources/views/Admin/ShippingMethod/_image.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% if shipping_method.image is not empty and shipping_method.image.path is not empty %}
{% set path = app.user.avatar.path%}
<p>{{ "bitbag_sylius_inpost_plugin.ui.file" | trans }} : {{ path ?? "empty" }}</p>
{% else %}
<i class="ui shipping"></i>
{% endif %}
2 changes: 1 addition & 1 deletion tests/Application/.env.test
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
APP_SECRET='ch4mb3r0f5ecr3ts'
DATABASE_URL='sqlite:///%kernel.project_dir%/var/data.db'

KERNEL_CLASS='Tests\Acme\SyliusExamplePlugin\Application\Kernel'
KERNEL_CLASS='Tests\BitBag\SyliusInPostPlugin\Application\Kernel'
6 changes: 6 additions & 0 deletions tests/Application/config/packages/_sylius.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ sylius_order:
order:
classes:
model: Tests\BitBag\SyliusInPostPlugin\Application\src\Entity\Order

sylius_shipping:
resources:
shipping_method:
classes:
model: Tests\BitBag\SyliusInPostPlugin\Application\src\Entity\ShippingMethod
Loading