<commit_before><?php

namespace Playbloom\Satisfy\Controller;

use Playbloom\Satisfy\Form\Type\ComposerLockType;
use Playbloom\Satisfy\Form\Type\DeleteFormType;
use Playbloom\Satisfy\Form\Type\RepositoryType;
use Playbloom\Satisfy\Model\Repository;
use Playbloom\Satisfy\Service\LockProcessor;
use Playbloom\Satisfy\Service\Manager;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class RepositoryController extends AbstractProtectedController
{
    public function indexAction(): Response
    {
        $this->checkAccess();
        $this->checkEnvironment();

        $manager = $this->get(Manager::class);
        $config = $manager->getConfig();
        $repositories = $manager->getRepositories();
        $satisRepository = [
            'type' => 'composer',
            'url' => $config->getHomepage(),
        ];

        $config = [
            'repositories' => [$satisRepository],
        ];

        return $this->render('@PlaybloomSatisfy/home.html.twig', compact('config', 'repositories'));
    }

    public function newAction(Request $request): Response
    {
        $this->checkAccess();

        $repository = new Repository();
        $form = $this->createForm(
            RepositoryType::class,
            $repository
        );

        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            try {
                $this->get(Manager::class)->add($form->getData());

                return $this->redirectToRoute('repository');
            } catch (\Exception $e) {
                $form->addError(new FormError($e->getMessage()));
            }
        }

        return $this->render('@PlaybloomSatisfy/new.html.twig', ['form' => $form->createView()]);
    }

    /**
     * @return RedirectResponse|Response
     */
    public function uploadAction(Request $request): Response
    {
        $this->checkAccess();

        $form = $this->createForm(ComposerLockType::class);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            try {
                $lock = $form->get('file')->getData()->openFile();
                $this->get(LockProcessor::class)->processFile($lock);

                return $this->redirectToRoute('repository');
            } catch (\Exception $e) {
                $form->addError(new FormError($e->getMessage()));
            }
        }

        return $this->render('@PlaybloomSatisfy/upload.html.twig', ['form' => $form->createView()]);
    }

    public function editAction(Request $request): Response
    {
        $this->checkAccess();
        $manager = $this->get(Manager::class);
        $repository = $manager->findOneRepository($request->attributes->get('repository'));
        if (!$repository) {
            return $this->redirectToRoute('repository');
        }

        $form = $this->createForm(
            RepositoryType::class,
            clone $repository
        );
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            try {
                $manager->update($repository, $form->getData());

                return $this->redirectToRoute('repository');
            } catch (\Exception $e) {
                $form->addError(new FormError($e->getMessage()));
            }
        }

        return $this->render('@PlaybloomSatisfy/edit.html.twig', ['form' => $form->createView()]);
    }

    public function deleteAction(Request $request): Response
    {
        $this->checkAccess();
        $manager = $this->get(Manager::class);
        $repository = $manager->findOneRepository($request->attributes->get('repository'));
        if (!$repository) {
            return $this->redirectToRoute('repository');
        }

        $form = $this->createForm(DeleteFormType::class);
        if (Request::METHOD_DELETE === $request->getMethod()) {
            try {
                $manager->delete($repository);

                return $this->redirectToRoute('repository');
            } catch (\Exception $e) {
                $form->addError(new FormError($e->getMessage()));
            }
        }<commit_msg>fix exception on lock file upload due to limited service locator visibility<commit_after><?php

namespace Playbloom\Satisfy\Controller;

use Playbloom\Satisfy\Form\Type\ComposerLockType;
use Playbloom\Satisfy\Form\Type\DeleteFormType;
use Playbloom\Satisfy\Form\Type\RepositoryType;
use Playbloom\Satisfy\Model\Repository;
use Playbloom\Satisfy\Service\LockProcessor;
use Playbloom\Satisfy\Service\Manager;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class RepositoryController extends AbstractProtectedController
{
    /** @var Manager */
    private $manager;

    /** @var LockProcessor */
    private $lockProcessor;

    public function __construct(Manager $manager, LockProcessor $lockProcessor)
    {
        $this->manager = $manager;
        $this->lockProcessor = $lockProcessor;
    }

    public function indexAction(): Response
    {
        $this->checkAccess();
        $this->checkEnvironment();

        $config = $this->manager->getConfig();
        $repositories = $this->manager->getRepositories();
        $satisRepository = [
            'type' => 'composer',
            'url' => $config->getHomepage(),
        ];

        $config = [
            'repositories' => [$satisRepository],
        ];

        return $this->render('@PlaybloomSatisfy/home.html.twig', compact('config', 'repositories'));
    }

    public function newAction(Request $request): Response
    {
        $this->checkAccess();

        $repository = new Repository();
        $form = $this->createForm(
            RepositoryType::class,
            $repository
        );

        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            try {
                $this->manager->add($form->getData());

                return $this->redirectToRoute('repository');
            } catch (\Exception $e) {
                $form->addError(new FormError($e->getMessage()));
            }
        }

        return $this->render('@PlaybloomSatisfy/new.html.twig', ['form' => $form->createView()]);
    }

    /**
     * @return RedirectResponse|Response
     */
    public function uploadAction(Request $request): Response
    {
        $this->checkAccess();

        $form = $this->createForm(ComposerLockType::class);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            try {
                $lock = $form->get('file')->getData()->openFile();
                $this->lockProcessor->processFile($lock);

                return $this->redirectToRoute('repository');
            } catch (\Exception $e) {
                $form->addError(new FormError($e->getMessage()));
            }
        }

        return $this->render('@PlaybloomSatisfy/upload.html.twig', ['form' => $form->createView()]);
    }

    public function editAction(Request $request): Response
    {
        $this->checkAccess();
        $repository = $this->manager->findOneRepository($request->attributes->get('repository'));
        if (!$repository) {
            return $this->redirectToRoute('repository');
        }

        $form = $this->createForm(
            RepositoryType::class,
            clone $repository
        );
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            try {
                $this->manager->update($repository, $form->getData());

                return $this->redirectToRoute('repository');
            } catch (\Exception $e) {
                $form->addError(new FormError($e->getMessage()));
            }
        }

        return $this->render('@PlaybloomSatisfy/edit.html.twig', ['form' => $form->createView()]);
    }

    public function deleteAction(Request $request): Response
    {
        $this->checkAccess();
        $repository = $this->manager->findOneRepository($request->attributes->get('repository'));
        if (!$repository) {
            return $this->redirectToRoute('repository');
        }

        $form = $this->createForm(DeleteFormType::class);
        if (Request::METHOD_DELETE === $request->getMethod()) {
            try {
                $this->manager->delete($repository);

                return $this->redirectToRoute('repository');
            } catch (\Exception $e) {
                $form->addError(new FormError($e->getMessage()));
            }
        }
{'fraction_non_alphanumeric': 0.15517241379310345, 'fraction_numerical': 0.0, 'mean_word_length': 2.3962776269872044, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 148, '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': '4937069', 'n_tokens_mistral': 2381, 'n_tokens_neox': 2320, 'n_words': 373}