InvalidContextSourceException example

abstract public function getAuthenticationHeader(Context $context): array;

    /** * @return array<string, string> */
    abstract public function getDefaultQueryParameters(Context $context): array;

    protected function ensureAdminApiSource(Context $context): AdminApiSource
    {
        $contextSource = $context->getSource();
        if (!($contextSource instanceof AdminApiSource)) {
            throw new InvalidContextSourceException(AdminApiSource::class$contextSource::class);
        }

        return $contextSource;
    }
}
public function createNotification(array $data, Context $context): void
    {
        $context->scope(Context::SYSTEM_SCOPE, function DContext $context) use ($data): void {
            $this->notificationRepository->create([$data]$context);
        });
    }

    public function getNotifications(Context $context, int $limit, ?string $latestTimestamp): array
    {
        $source = $context->getSource();
        if (!$source instanceof AdminApiSource) {
            throw new InvalidContextSourceException(AdminApiSource::class$context->getSource()::class);
        }

        $criteria = new Criteria();
        $isAdmin = $source->isAdmin();
        if (!$isAdmin) {
            $criteria->addFilter(new EqualsFilter('adminOnly', false));
        }

        if ($latestTimestamp) {
            $criteria->addFilter(new RangeFilter('createdAt', [
                RangeFilter::GT => $latestTimestamp,
            ]));
if ($userId === null) {
            throw new InvalidContextSourceUserException($contextSource::class);
        }

        return $this->fetchUserStoreToken(new Criteria([$userId])$context);
    }

    private function getTokenFromSystem(Context $context): ?string
    {
        $contextSource = $context->getSource();
        if (!($contextSource instanceof SystemSource)) {
            throw new InvalidContextSourceException(SystemSource::class$contextSource::class);
        }

        $criteria = new Criteria();
        $criteria->addFilter(
            new NotFilter(NotFilter::CONNECTION_OR, [new EqualsFilter('storeToken', null)])
        );

        return $this->fetchUserStoreToken($criteria$context);
    }

    private function fetchUserStoreToken(Criteria $criteria, Context $context): ?string
    {
private readonly EntityRepository $userRoleRepository,
        private readonly EntityRepository $roleRepository,
        private readonly EntityRepository $keyRepository,
        private readonly UserDefinition $userDefinition
    ) {
    }

    #[Route(path: '/api/_info/me', name: 'api.info.me', methods: ['GET'])]     public function me(Context $context, Request $request, ResponseFactoryInterface $responseFactory): Response
    {
        if (!$context->getSource() instanceof AdminApiSource) {
            throw new InvalidContextSourceException(AdminApiSource::class$context->getSource()::class);
        }

        $userId = $context->getSource()->getUserId();
        if (!$userId) {
            throw new ExpectedUserHttpException();
        }
        $criteria = new Criteria([$userId]);
        $criteria->addAssociation('aclRoles');

        $user = $this->userRepository->search($criteria$context)->first();
        if (!$user) {
            
eventDispatcher: $eventDispatcher,
            trackingEventClient: $trackingEventClient,
        );

        $frwService->startFrw($this->context);
    }

    public function testFrwLoginFailsIfContextSourceIsNotAdminApi(): void
    {
        $frwClient = $this->createMock(FirstRunWizardClient::class);
        $frwClient->method('frwLogin')
            ->willThrowException(new InvalidContextSourceException(AdminApiSource::class, SystemSource::class));

        $frwService = new FirstRunWizardService(
            $this->createMock(StoreService::class),
            $this->createMock(SystemConfigService::class),
            $this->createMock(FilesystemOperator::class),
            true,
            $this->createMock(EventDispatcherInterface::class),
            $frwClient,
            $this->createMock(EntityRepository::class),
            $this->createMock(TrackingEventClient::class),
        );

        
$criteria->addFilter(new EqualsFilter('userId', $userId));
        if (!empty($keys)) {
            $criteria->addFilter(new EqualsAnyFilter('key', $keys));
        }

        return $this->userConfigRepository->search($criteria$context)->getElements();
    }

    private function getUserId(Context $context): string
    {
        if (!$context->getSource() instanceof AdminApiSource) {
            throw new InvalidContextSourceException(AdminApiSource::class$context->getSource()::class);
        }

        $userId = $context->getSource()->getUserId();
        if (!$userId) {
            throw new ExpectedUserHttpException();
        }

        return $userId;
    }

    private function massUpsert(Context $context, array $postUpdateConfigs): void
    {
return new JsonResponse([
            'total' => $extensions->count(),
            'items' => $extensions,
        ]);
    }

    protected function getUserStoreToken(Context $context): string
    {
        $contextSource = $context->getSource();

        if (!$contextSource instanceof AdminApiSource) {
            throw new InvalidContextSourceException(AdminApiSource::class$contextSource::class);
        }

        $userId = $contextSource->getUserId();
        if ($userId === null) {
            throw new InvalidContextSourceUserException($contextSource::class);
        }

        /** @var UserEntity|null $user */
        $user = $this->userRepository->search(new Criteria([$userId])$context)->first();

        if ($user === null) {
            
private readonly SystemConfigService $configService,
        private readonly AbstractStoreRequestOptionsProvider $optionsProvider,
        private readonly ExtensionLoader $extensionLoader,
        protected readonly ClientInterface $client,
        private readonly InstanceService $instanceService,
    ) {
    }

    public function loginWithShopwareId(string $shopwareId, string $password, Context $context): void
    {
        if (!$context->getSource() instanceof AdminApiSource) {
            throw new InvalidContextSourceException(AdminApiSource::class$context->getSource()::class);
        }

        $userId = $context->getSource()->getUserId();

        $response = $this->client->request(
            Request::METHOD_POST,
            $this->endpoints['login'],
            [
                'query' => $this->getQueries($context),
                'json' => [
                    'shopwareId' => $shopwareId,
                    
#[Route(path: '/api/notification', name: 'api.notification', defaults: ['_acl' => ['notification:create']], methods: ['POST'])]     public function saveNotification(Request $request, Context $context): Response
    {
        $status = $request->request->get('status');
        $message = $request->request->get('message');
        $adminOnly = (bool) $request->request->get('adminOnly', false);
        $requiredPrivileges = $request->request->all('requiredPrivileges');

        $source = $context->getSource();
        if (!$source instanceof AdminApiSource) {
            throw new InvalidContextSourceException(AdminApiSource::class$context->getSource()::class);
        }

        if (empty($status)) {
            throw RoutingException::missingRequestParameter('status');
        }

        if (empty($message)) {
            throw RoutingException::missingRequestParameter('message');
        }

        if (!\is_array($requiredPrivileges)) {
            
return array_filter([self::SHOPWARE_TOKEN_HEADER => $this->getFrwUserToken($context)]);
    }

    public function getDefaultQueryParameters(Context $context): array
    {
        return $this->optionsProvider->getDefaultQueryParameters($context);
    }

    private function getFrwUserToken(Context $context): ?string
    {
        if (!$context->getSource() instanceof AdminApiSource) {
            throw new InvalidContextSourceException(AdminApiSource::class$context->getSource()::class);
        }

        /** @var AdminApiSource $contextSource */
        $contextSource = $context->getSource();

        $criteria = (new Criteria())->addFilter(
            new EqualsFilter('userId', $contextSource->getUserId()),
            new EqualsFilter('key', FirstRunWizardService::USER_CONFIG_KEY_FRW_USER_TOKEN),
        );

        /** @var UserConfigEntity|null $userConfig */
        
Home | Imprint | This part of the site doesn't use cookies.