Ejemplo n.º 1
0
    /**
     * Build and cache url by requested path and parameters
     *
     * @param   string|null $routePath
     * @param   array|null $routeParams
     * @return  string
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    public function getUrl($routePath = null, $routeParams = null)
    {
        if (filter_var($routePath, FILTER_VALIDATE_URL)) {
            return $routePath;
        }

        $routeParams = $this->routeParamsPreprocessor
            ->execute($this->_scopeResolver->getAreaCode(), $routePath, $routeParams);

        $isCached = true;
        $isArray = is_array($routeParams);

        if ($isArray) {
            array_walk_recursive(
                $routeParams,
                function ($item) use (&$isCached) {
                    if (is_object($item)) {
                        $isCached = false;
                    }
                }
            );
        }

        if(!$isCached) {
            return $this->getUrlModifier()->execute(
                $this->createUrl($routePath, $routeParams),
                $routeParams
            );
        }

        $cashedParams = $routeParams;
        if ($isArray) {
            ksort($cashedParams);
        }

        $cacheKey = md5($routePath . serialize($cashedParams));
        if (!isset($this->cacheUrl[$cacheKey])) {
            $this->cacheUrl[$cacheKey] = $this->getUrlModifier()->execute(
                $this->createUrl($routePath, $routeParams),
                $routeParams
            );
        }

        return $this->cacheUrl[$cacheKey];
    }