Skip to content
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

Integration with a Symfony Workflow component #71

Open
fre5h opened this issue Oct 8, 2016 · 2 comments
Open

Integration with a Symfony Workflow component #71

fre5h opened this issue Oct 8, 2016 · 2 comments
Assignees

Comments

@fre5h
Copy link
Owner

fre5h commented Oct 8, 2016

Symfony Workflow component appeared in version 3.2. It is the implementation of the state machine. As ENUM fields in database often used for keeping some readable statuses, so it would be nice to add state machine over these types in PHP.
I am not sure if this integration is possible. But want to investigate.

@fre5h fre5h added this to the 5.0 version milestone Oct 8, 2016
@fre5h fre5h self-assigned this Oct 8, 2016
@fre5h fre5h modified the milestones: 5.1, 5.0 Jan 9, 2017
@fre5h fre5h modified the milestones: 5.2, 6.0, 6.1 Nov 12, 2017
@kubaceg
Copy link

kubaceg commented Jan 19, 2018

It will be really nice feature :) 👍

@kubaceg
Copy link

kubaceg commented Jan 19, 2018

Ok It's possible. In workflow configuration You must indicate Your custom marking store:

marking_store:
    service: App\Service\MarkingStore

And here is code of this class

<?php
/**
 * @author Jakub Cegiełka <kuba.ceg@gmail.com>
 */

namespace App\Service;

use App\Entity\Offer\OfferStatus;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Workflow\Marking;
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;

class MarkingStore implements MarkingStoreInterface
{
    private $property;
    private $propertyAccessor;

    /**
     * @param string                         $property
     * @param PropertyAccessorInterface|null $propertyAccessor
     */
    public function __construct($property = 'status', PropertyAccessorInterface $propertyAccessor = null)
    {
        $this->property = $property;
        $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
    }

    /**
     * {@inheritdoc}
     */
    public function getMarking($subject)
    {
        $placeName = $this->propertyAccessor->getValue($subject, $this->property);
        $placeName = OfferStatus::getReadableValue($placeName);

        if (!$placeName) {
            return new Marking();
        }

        return new Marking(array($placeName => 1));
    }

    /**
     * {@inheritdoc}
     */
    public function setMarking($subject, Marking $marking)
    {
        $value = key($marking->getPlaces());
        $placeValue = OfferStatus::getChoices()[$value];

        $this->propertyAccessor->setValue($subject, $this->property, $placeValue);
    }
}

As You see in constructor I've indicated column name with our enum type (property arg). If there (in wolkflow component) will be possibility to select column with our field this service can be automated. At this moment it's not possible.

@fre5h fre5h removed this from the 6.1 milestone Apr 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants