protected function createSpMetadataFile()
    {
        $url = $this->getConfig()->getIdpEntityID();

        $entityID = $this->getConfig()->getEntityID();
        $metadataProvider = new MetadataProvider([MetadataProvider::OPTION_BASEURL => $url]);

        try {
            $message = $metadataProvider->getSPEntityConfiguration($entityID);
        } catch (\Exception $e) {
            $message = new SpEntityConfigurationMessage();
        }

        if (empty($message->getXml())) {
            if (!$message->getId()) {
                if (!$this->validateIdpMetadata()) {
                    throw new \Exception('Unable to send registration request due to not found IdP metadata file');
                }

                $idp = (new Metadata())->createEntityDescriptor(
                    file_get_contents(
                        $this->getConfig()->getSamlMetadataBaseDir() . '/' . $this->getConfig()->getIdpMetadataFile()
                    )
                )->getFirstIdpSsoDescriptor();

                $metadataProvider->subscribeApplication($this->getConfig(), $idp);
            }

            return false;
        }

        $path = $this->getConfig()->getSamlMetadataBaseDir() . '/' . $this->getConfig()->getSpMetadataFile();
        $this->createDir($path);

        if (@file_put_contents($path, $message->getXml()) === false) {
            throw new \Exception('Error in Sp MetadataFile writing');
        }

        return true;
    }