<commit_before><?php

/*
 * This file is part of the TNTExpress package.
 *
 * (c) <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace winzou\Bundle\TNTExpressBundle\Tracking;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use winzou\Bundle\TNTExpressBundle\Entity\Events;
use winzou\Bundle\TNTExpressBundle\Entity\ParcelResponse;

class DispatchTrackingManager
{
    /**
     * @var TrackingManager
     */
    protected $trackingManager;

    /**
     * @var EventDispatcherInterface
     */
    protected $dispatcher;

    protected $propertyAccessor;

    public function __construct(TrackingManager $trackingManager, EventDispatcherInterface $dispatcher, PropertyAccessorInterface $propertyAccessor = null)
    {
        $this->trackingManager  = $trackingManager;
        $this->dispatcher       = $dispatcher;
        $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
    }

    /**
     * Update the events field of the given parcelResponse according to SOAP service response
     *
     * @param ParcelResponse $parcelResponse
     */
    public function updateEvents(ParcelResponse $parcelResponse)
    {
        $lastEvent = $parcelResponse->getEvents() ? $parcelResponse->getEvents()->getLastEvent() : Events::$sequence[0];

        $this->trackingManager->updateEvents($parcelResponse);

        if (null !== $parcelResponse->getEvents() && $lastEvent !== $parcelResponse->getEvents()->getLastEvent()) {
            $this->dispatchEvents($parcelResponse, $lastEvent);
        }
    }

    /**
     * Dispatch events for a given parcelResponse from a certain event to the last not null event
     *
     * @param ParcelResponse $parcelResponse
     * @param string|null    $fromEvent
     */
    public function dispatchEvents(ParcelResponse $parcelResponse, $fromEvent = null)
    {
        $genericEvent = new GenericEvent($parcelResponse);
        $startIndex   = array_search($fromEvent, Events::$sequence) ?: array_search($parcelResponse->getEvents()->getLastEvent(), Events::$sequence);

        foreach (Events::$sequence as $i => $event) {
            if ($i <= $startIndex && $parcelResponse->getEvents()->isEventDone($event)) {
                $this->dispatcher->dispatch(
                    sprintf('winzou.tntexpress.events.%s', $event),
                    $genericEvent
                );
            }
        }
    }
}
<commit_msg>Fix condition for dispatching events<commit_after><?php

/*
 * This file is part of the TNTExpress package.
 *
 * (c) <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace winzou\Bundle\TNTExpressBundle\Tracking;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use winzou\Bundle\TNTExpressBundle\Entity\Events;
use winzou\Bundle\TNTExpressBundle\Entity\ParcelResponse;

class DispatchTrackingManager
{
    /**
     * @var TrackingManager
     */
    protected $trackingManager;

    /**
     * @var EventDispatcherInterface
     */
    protected $dispatcher;

    protected $propertyAccessor;

    public function __construct(TrackingManager $trackingManager, EventDispatcherInterface $dispatcher, PropertyAccessorInterface $propertyAccessor = null)
    {
        $this->trackingManager  = $trackingManager;
        $this->dispatcher       = $dispatcher;
        $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
    }

    /**
     * Update the events field of the given parcelResponse according to SOAP service response
     *
     * @param ParcelResponse $parcelResponse
     */
    public function updateEvents(ParcelResponse $parcelResponse)
    {
        $lastEvent = $parcelResponse->getEvents() ? $parcelResponse->getEvents()->getLastEvent() : Events::$sequence[0];

        $this->trackingManager->updateEvents($parcelResponse);

        if (null !== $parcelResponse->getEvents() && $lastEvent !== $parcelResponse->getEvents()->getLastEvent()) {
            $this->dispatchEvents($parcelResponse, $lastEvent);
        }
    }

    /**
     * Dispatch events for a given parcelResponse from a certain event to the last not null event
     *
     * @param ParcelResponse $parcelResponse
     * @param string|null    $fromEvent
     */
    public function dispatchEvents(ParcelResponse $parcelResponse, $fromEvent = null)
    {
        $genericEvent = new GenericEvent($parcelResponse);
        $startIndex   = array_search($fromEvent, Events::$sequence) ?: -1;

        foreach (Events::$sequence as $i => $event) {
            if ($i > $startIndex && $parcelResponse->getEvents()->isEventDone($event)) {
                $this->dispatcher->dispatch(
                    sprintf('winzou.tntexpress.events.%s', $event),
                    $genericEvent
                );
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.10923105846963715, 'fraction_numerical': 0.0005640157924421884, 'mean_word_length': 3.2594075260208166, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5611505', 'n_tokens_mistral': 1426, 'n_tokens_neox': 1386, 'n_words': 309}