<commit_before><?php

namespace Annex\TenantBundle\Controller;

use Annex\TenantBundle\Entity\Setting;
use Annex\TenantBundle\Entity\Tenant;
use Doctrine\DBAL\Migrations\Configuration\Configuration;
use Doctrine\DBAL\Migrations\Migration;
use Doctrine\DBAL\Migrations\OutputWriter;

use Postmark\PostmarkClient;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class LaunchController extends Controller
{

    /**
     * @Route("update", name="auto_update")
     */
    public function updateDatabase(Request $request)
    {
        $this->updateSchema();
        return $this->redirect($this->generateUrl('account_settings'));
    }

    /**
     * Signup is required before launch to create an empty database and entry in tenant
     * @Route("launch", name="launch")
     */
    public function launchTenant(Request $request)
    {
        /** @var \Annex\TenantBundle\Services\TenantService $tenantService */
        $tenantService = $this->container->get('annex_tenant.tenant_information');

        /** @var $tenant \Annex\TenantBundle\Entity\Tenant */
        if (!$tenant = $tenantService->getTenant($this->get('session')->get('tenantId'))) {
            die("Could not find an account for ".$request->get('accountCode'));
        }

        /** @var \Annex\TenantBundle\Services\Brightpearl\Utility $utilityService */
        $utilityService = $this->get('service.brightpearl.utility');
        $utilityService->setTenant($tenant);

        $appDomain = $this->getParameter('app_info.tld');

        // Go to Brightpearl to see if this user has installed the app
        if (!$token = $utilityService->getBrightpearlToken($tenant->getBrightpearlAccountCode())) {
            die("Failed to get install info from Brightpearl");
        }

        // Save the token into the tenant database
        $tenant->setBrightpearlToken($token);
        $tenant->setStatus('TRIAL');

        if ($tenantService->updateTenant()) {

            // We need to already have an empty database
            // Run any migrations that need running
            $this->updateSchema();
            $this->addAdminUser($tenant);
            $this->addUser($tenant);
            $this->sendActivationEmail($tenant);

            if ($this->getParameter("kernel.environment") == 'prod') {
                return $this->redirect("http://{$tenant->getBrightpearlAccountCode()}.{$appDomain}/login?launched=1");
            } else {
                return $this->redirect("http://localhost:8000/login?launched=1");
            }
        } else {
            die("Could not update tenant with token");
        }

    }

    /**
     * @param \Annex\TenantBundle\Entity\Tenant $tenant
     */
    private function sendActivationEmail($tenant) {

        try {

            $client = new PostmarkClient($this->getParameter('annex.postmark_api_key'));
            $message = $this->renderView(
                'emails/basic.html.twig',
                [
                    'tenant' => $tenant,
                    'message' => 'Account "'.$tenant->getName().'" activated'
                ]
            );
            $client->sendEmail(
                "<EMAIL>",
                "<EMAIL>",
                "Annex account activated",
                $message
            );

        } catch (\Exception $generalException) {<commit_msg>Prevent activation if already launched<commit_after><?php

namespace Annex\TenantBundle\Controller;

use Annex\TenantBundle\Entity\Setting;
use Annex\TenantBundle\Entity\Tenant;
use Doctrine\DBAL\Migrations\Configuration\Configuration;
use Doctrine\DBAL\Migrations\Migration;
use Doctrine\DBAL\Migrations\OutputWriter;

use Postmark\PostmarkClient;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class LaunchController extends Controller
{

    /**
     * @Route("update", name="auto_update")
     */
    public function updateDatabase(Request $request)
    {
        $this->updateSchema();
        return $this->redirect($this->generateUrl('account_settings'));
    }

    /**
     * Signup is required before launch to create an empty database and entry in tenant
     * @Route("launch", name="launch")
     */
    public function launchTenant(Request $request)
    {
        /** @var \Annex\TenantBundle\Services\TenantService $tenantService */
        $tenantService = $this->container->get('annex_tenant.tenant_information');

        /** @var $tenant \Annex\TenantBundle\Entity\Tenant */
        if (!$tenant = $tenantService->getTenant($this->get('session')->get('tenantId'))) {
            die("Could not find an account for ".$request->get('accountCode'));
        }

        /** @var \Annex\TenantBundle\Services\Brightpearl\Utility $utilityService */
        $utilityService = $this->get('service.brightpearl.utility');
        $utilityService->setTenant($tenant);

        $appDomain = $this->getParameter('app_info.tld');

        // Go to Brightpearl to see if this user has installed the app
        if (!$token = $utilityService->getBrightpearlToken($tenant->getBrightpearlAccountCode())) {
            die("Failed to get install info from Brightpearl - please ensure you have installed the app via Brightpearl before activating.");
        }

        // Already activated
        if ($tenant->getStatus() == 'TRIAL') {
            return $this->redirect("http://{$tenant->getBrightpearlAccountCode()}.{$appDomain}/login");
        }

        // Save the token into the tenant database
        $tenant->setBrightpearlToken($token);
        $tenant->setStatus('TRIAL');

        if ($tenantService->updateTenant()) {

            // We need to already have an empty database
            // Run any migrations that need running
            $this->updateSchema();
            $this->addAdminUser($tenant);
            $this->addUser($tenant);
            $this->sendActivationEmail($tenant);

            if ($this->getParameter("kernel.environment") == 'prod') {
                return $this->redirect("http://{$tenant->getBrightpearlAccountCode()}.{$appDomain}/login?launched=1");
            } else {
                return $this->redirect("http://{$tenant->getBrightpearlAccountCode()}.localhost:8000/login?launched=1");
            }
        } else {
            die("Could not update tenant with token");
        }

    }

    /**
     * @param \Annex\TenantBundle\Entity\Tenant $tenant
     */
    private function sendActivationEmail($tenant) {

        try {

            $client = new PostmarkClient($this->getParameter('annex.postmark_api_key'));
            $message = $this->renderView(
                'emails/basic.html.twig',
                [
                    'tenant' => $tenant,
                    'message' => 'Account "'.$tenant->getName().'" activated'
                ]
            );
            $client->sendEmail(
                "<EMAIL>",
                "<EMAIL>",
                "Annex account activated",
                $message
            );

        } catch (\Exception $generalException) {
{'fraction_non_alphanumeric': 0.13520656558631242, 'fraction_numerical': 0.0016692168590902768, 'mean_word_length': 2.450095969289827, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 73, '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': '7564994', 'n_tokens_mistral': 1960, 'n_tokens_neox': 1947, 'n_words': 372}