<commit_before><?php
namespace Extia\Bundle\MissionBundle\Form\Handler;

use Extia\Bundle\NotificationBundle\Notification\NotifierInterface;

use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Log\LoggerInterface;

/**
 * handler for mission form
 *
 * @see Extia/Bundles/MissionBundle/Resources/config/admin.xml
 */
class MissionHandler
{
    protected $notifier;
    protected $rootDir;
    protected $logger;
    protected $debug;

    /**
     * construct
     * @param NotifierInterface $notifier
     * @param string            $rootDir
     * @param LoggerInterface   $logger
     * @param bool              $debug
     */
    public function __construct(NotifierInterface $notifier, $rootDir, LoggerInterface $logger, $debug)
    {
        $this->notifier        = $notifier;
        $this->rootDir         = $rootDir;
        $this->logger          = $logger;
        $this->debug           = $debug;
    }

    /**
     * @param Form    $form
     * @param Request $request
     * @param Pdo     $pdo     opt pdo connection
     *
     * @return bool
     */
    public function handle(Form $form, Request $request, \Pdo $pdo = null)
    {
        $form->submit($request);
        if (!$form->isValid()) {
            return false;
        }

        if (empty($pdo)) {
            $pdo = \Propel::getConnection('default');
        }

        $pdo->beginTransaction();
        $mission = $form->getData();

        try {
            // image uploading
            if ($form->get('client')->has('image') && $image = $form->get('client')->get('image')->getData()) {
                try {
                    $extension = $image->guessExtension();
                    if (!in_array($extension, array('jpeg', 'png'))) {
                        // $this->notifier->add('warning', 'consultant.admin.notifications.invalid_image');
                    } else {
                        $fileName = $mission->getClient()->getSlug().'.'.$extension;
                        $webPath  = 'images/logos/';
                        $path     = sprintf('%s/../web/%s', $this->rootDir, $webPath);

                        if (!is_dir($path)) {
                            mkdir($path); // will throw an error if access denied caught below
                        }

                        $physicalDoc = $image->move($path, $fileName);
                        $mission->getClient()->setImage($webPath.$fileName);
                    }
                } catch (\Exception $e) {
                    // if ($this->debug) {
                        $pdo->rollback();


                        var_dump($e);
                        die;

                        throw $e;
                    // }

                    // $this->logger->err($e->getMessage());
                    // $this->notifier->add('error', 'consultant.admin.notifications.error_image');
                }
            }

            if ($form->has('client_id') && $clientId = $form->get('client_id')->getData()) {
                $mission->setClientId($clientId);
            } elseif ($form->has('client') && $client = $form->get('client')->getData()) {
                $mission->setClient($client);
            } else {
                // notifier warning
                return false;
            }

            $mission->save($pdo);

            $pdo->commit();

            return true;

        } catch(\Exception $e) {
            $pdo->rollback();

            return false;
        }
    }
}<commit_msg>fix missing type on mission creation<commit_after><?php
namespace Extia\Bundle\MissionBundle\Form\Handler;

use Extia\Bundle\NotificationBundle\Notification\NotifierInterface;

use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Log\LoggerInterface;

/**
 * handler for mission form
 *
 * @see Extia/Bundles/MissionBundle/Resources/config/admin.xml
 */
class MissionHandler
{
    protected $notifier;
    protected $rootDir;
    protected $logger;
    protected $debug;

    /**
     * construct
     * @param NotifierInterface $notifier
     * @param string            $rootDir
     * @param LoggerInterface   $logger
     * @param bool              $debug
     */
    public function __construct(NotifierInterface $notifier, $rootDir, LoggerInterface $logger, $debug)
    {
        $this->notifier        = $notifier;
        $this->rootDir         = $rootDir;
        $this->logger          = $logger;
        $this->debug           = $debug;
    }

    /**
     * @param Form    $form
     * @param Request $request
     * @param Pdo     $pdo     opt pdo connection
     *
     * @return bool
     */
    public function handle(Form $form, Request $request, \Pdo $pdo = null)
    {
        $form->submit($request);
        if (!$form->isValid()) {
            return false;
        }

        if (empty($pdo)) {
            $pdo = \Propel::getConnection('default');
        }

        $pdo->beginTransaction();
        $mission = $form->getData();

        try {
            // image uploading
            if ($form->get('client')->has('image') && $image = $form->get('client')->get('image')->getData()) {
                try {
                    $extension = $image->guessExtension();
                    if (!in_array($extension, array('jpeg', 'png'))) {
                        // $this->notifier->add('warning', 'consultant.admin.notifications.invalid_image');
                    } else {
                        $fileName = $mission->getClient()->getSlug().'.'.$extension;
                        $webPath  = 'images/logos/';
                        $path     = sprintf('%s/../web/%s', $this->rootDir, $webPath);

                        if (!is_dir($path)) {
                            mkdir($path); // will throw an error if access denied caught below
                        }

                        $physicalDoc = $image->move($path, $fileName);
                        $mission->getClient()->setImage($webPath.$fileName);
                    }
                } catch (\Exception $e) {
                    // if ($this->debug) {
                        $pdo->rollback();


                        var_dump($e);
                        die;

                        throw $e;
                    // }

                    // $this->logger->err($e->getMessage());
                    // $this->notifier->add('error', 'consultant.admin.notifications.error_image');
                }
            }

            if ($form->has('client_id') && $clientId = $form->get('client_id')->getData()) {
                $mission->setClientId($clientId);
            } elseif ($form->has('client') && $client = $form->get('client')->getData()) {
                $mission->setClient($client);
            } else {
                // notifier warning
                return false;
            }

            // only client missions can be created
            $mission->setType('client');

            $mission->save($pdo);

            $pdo->commit();

            return true;

        } catch(\Exception $e) {
            $pdo->rollback();

            return false;
        }
    }
}
{'fraction_non_alphanumeric': 0.14492753623188406, 'fraction_numerical': 0.0, 'mean_word_length': 1.4731219848380428, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 84, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '137049', 'n_tokens_mistral': 1934, 'n_tokens_neox': 1861, 'n_words': 350}