<commit_before><?php

namespace Jinya\Controller\Api\Theme;

use Jinya\Formatter\Theme\ThemeFormatterInterface;
use Jinya\Framework\BaseApiController;
use Jinya\Services\Theme\ThemeLinkServiceInterface;
use Jinya\Services\Theme\ThemeServiceInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class ThemeLinkController extends BaseApiController
{
    /**
     * @Route("/api/theme/{themeName}/links", methods={"GET"}, name="api_theme_get_links")
     * @IsGranted("ROLE_WRITER")
     *
     * @param string $themeName
     * @param ThemeServiceInterface $themeService
     * @param ThemeFormatterInterface $themeFormatter
     * @return Response
     */
    public function getAction(
        string $themeName,
        ThemeServiceInterface $themeService,
        ThemeFormatterInterface $themeFormatter
    ): Response {
        [$data, $status] = $this->tryExecute(static function () use ($themeName, $themeService, $themeFormatter) {
            $theme = $themeService->getTheme($themeName);

            return $themeFormatter
                ->init($theme)
                ->links()
                ->format();
        });

        return $this->json($data, $status);
    }

    /**
     * @Route("/api/theme/{themeName}/links/structure", methods={"GET"}, name="api_theme_get_links_structure")
     * @IsGranted("ROLE_WRITER")
     *
     * @param string $themeName
     * @param ThemeLinkServiceInterface $themeLinkService
     * @return Response
     */
    public function getStructureAction(string $themeName, ThemeLinkServiceInterface $themeLinkService): Response
    {
        [$data, $status] = $this->tryExecute(static function () use ($themeName, $themeLinkService) {
            return $themeLinkService->getLinkConfigStructure($themeName);
        });

        return $this->json($data, $status);
    }

    /**
     * @Route("/api/theme/{themeName}/links", methods={"PUT"}, name="api_theme_put_links")
     * @IsGranted("ROLE_WRITER")
     *
     * @param string $themeName
     * @param ThemeLinkServiceInterface $themeLinkService
     * @return Response
     */
    public function putLinksAction(string $themeName, ThemeLinkServiceInterface $themeLinkService): Response
    {
        [$data, $status] = $this->tryExecute(function () use ($themeName, $themeLinkService) {
            $menus = $this->getValue('menus', []);
            $pages = $this->getValue('pages', []);
            $forms = $this->getValue('forms', []);
            $galleries = $this->getValue('galleries', []);
            $files = $this->getValue('files', []);
            $segmentPages = $this->getValue('segmentPages', []);

            foreach ($menus as $key => $menu) {
                if (array_key_exists('id', $menu)) {
                    $themeLinkService->saveMenu($key, $themeName, $menu['id']);
                }
            }

            foreach ($pages as $key => $page) {
                if (array_key_exists('slug', $page)) {
                    $themeLinkService->savePage($key, $themeName, $page['slug']);
                }
            }

            foreach ($forms as $key => $form) {
                if (array_key_exists('slug', $form)) {
                    $themeLinkService->saveForm($key, $themeName, $form['slug']);
                }
            }

            foreach ($galleries as $key => $gallery) {
                if (array_key_exists('slug', $gallery)) {
                    $themeLinkService->saveGallery($key, $themeName, $gallery['slug']);
                }
            }

            foreach ($files as $key => $file) {
                if (array_key_exists('slug', $file)) {
                    $themeLinkService->saveFile($key, $themeName, $file['slug']);
                }
            }

            foreach ($segmentPages as $key => $segmentPage) {
                if (array_key_exists('slug', $segmentPage)) {
                    $themeLinkService->saveSegmentPage($key, $themeName, $segmentPage['slug']);
                }
            }<commit_msg>CHORE: Fixed menu and file link saving<commit_after><?php

namespace Jinya\Controller\Api\Theme;

use Jinya\Formatter\Theme\ThemeFormatterInterface;
use Jinya\Framework\BaseApiController;
use Jinya\Services\Theme\ThemeLinkServiceInterface;
use Jinya\Services\Theme\ThemeServiceInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class ThemeLinkController extends BaseApiController
{
    /**
     * @Route("/api/theme/{themeName}/links", methods={"GET"}, name="api_theme_get_links")
     * @IsGranted("ROLE_WRITER")
     *
     * @param string $themeName
     * @param ThemeServiceInterface $themeService
     * @param ThemeFormatterInterface $themeFormatter
     * @return Response
     */
    public function getAction(
        string $themeName,
        ThemeServiceInterface $themeService,
        ThemeFormatterInterface $themeFormatter
    ): Response {
        [$data, $status] = $this->tryExecute(static function () use ($themeName, $themeService, $themeFormatter) {
            $theme = $themeService->getTheme($themeName);

            return $themeFormatter
                ->init($theme)
                ->links()
                ->format();
        });

        return $this->json($data, $status);
    }

    /**
     * @Route("/api/theme/{themeName}/links/structure", methods={"GET"}, name="api_theme_get_links_structure")
     * @IsGranted("ROLE_WRITER")
     *
     * @param string $themeName
     * @param ThemeLinkServiceInterface $themeLinkService
     * @return Response
     */
    public function getStructureAction(string $themeName, ThemeLinkServiceInterface $themeLinkService): Response
    {
        [$data, $status] = $this->tryExecute(static function () use ($themeName, $themeLinkService) {
            return $themeLinkService->getLinkConfigStructure($themeName);
        });

        return $this->json($data, $status);
    }

    /**
     * @Route("/api/theme/{themeName}/links", methods={"PUT"}, name="api_theme_put_links")
     * @IsGranted("ROLE_WRITER")
     *
     * @param string $themeName
     * @param ThemeLinkServiceInterface $themeLinkService
     * @return Response
     */
    public function putLinksAction(string $themeName, ThemeLinkServiceInterface $themeLinkService): Response
    {
        [$data, $status] = $this->tryExecute(function () use ($themeName, $themeLinkService) {
            $menus = $this->getValue('menus', []);
            $pages = $this->getValue('pages', []);
            $forms = $this->getValue('forms', []);
            $galleries = $this->getValue('galleries', []);
            $files = $this->getValue('files', []);
            $segmentPages = $this->getValue('segmentPages', []);

            foreach ($menus as $key => $menu) {
                if (array_key_exists('slug', $menu)) {
                    $themeLinkService->saveMenu($key, $themeName, $menu['slug']);
                } elseif (array_key_exists('id', $menu)) {
                    $themeLinkService->saveMenu($key, $themeName, $menu['id']);
                }
            }

            foreach ($pages as $key => $page) {
                if (array_key_exists('slug', $page)) {
                    $themeLinkService->savePage($key, $themeName, $page['slug']);
                }
            }

            foreach ($forms as $key => $form) {
                if (array_key_exists('slug', $form)) {
                    $themeLinkService->saveForm($key, $themeName, $form['slug']);
                }
            }

            foreach ($galleries as $key => $gallery) {
                if (array_key_exists('slug', $gallery)) {
                    $themeLinkService->saveGallery($key, $themeName, $gallery['slug']);
                }
            }

            foreach ($files as $key => $file) {
                if (array_key_exists('slug', $file)) {
                    $themeLinkService->saveFile($key, $themeName, $file['slug']);
                } elseif (array_key_exists('id', $file)) {
                    $themeLinkService->saveMenu($key, $themeName, $file['id']);
                }
            }

            foreach ($segmentPages as $key => $segmentPage) {
                if (array_key_exists('slug', $segmentPage)) {
                    $themeLinkService->saveSegmentPage($key, $themeName, $segmentPage['slug']);
                }
            }
{'fraction_non_alphanumeric': 0.14386376981796828, 'fraction_numerical': 0.0, 'mean_word_length': 2.3860834990059643, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, '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': '3826411', 'n_tokens_mistral': 2365, 'n_tokens_neox': 2295, 'n_words': 414}