core-bundle/src/OptIn/OptIn.php
public function purgeTokens(): void
    {
        /** @var OptInModel $adapter */
        $adapter = $this->framework->getAdapter(OptInModel::class);

        if (!$tokens = $adapter->findExpiredTokens()) {
            return;
        }

        /** @var Model $adapter */
        $adapter = $this->framework->getAdapter(Model::class);

        foreach ($tokens as $token) {
            $delete = true;
            $related = $token->getRelatedRecords();

            foreach ($related as $table => $id) {
                /** @var Model $model */
                $model = $this->framework->getAdapter($adapter->getClassFromTable($table));

                // Check if the related records still exist
                if (null !== $model->findMultipleByIds($id)) {
                    $delete = false;
                    break;
                }
            }

            if ($delete) {
                $token->delete();
            } else {
                // Prolong the token for another 3 years if the related records still exist
                $token->removeOn = strtotime('+3 years', (int) $token->removeOn);
                $token->save();
            }
        }
    }
public function purgeTokens(): void
    {
        /** @var OptInModel $adapter */
        $adapter = $this->framework->getAdapter(OptInModel::class);

        if (!$tokens = $adapter->findExpiredTokens()) {
            return;
        }

        /** @var Model $adapter */
        $adapter = $this->framework->getAdapter(Model::class);

        foreach ($tokens as $token) {
            $delete = true;
            $related = $token->getRelatedRecords();

            foreach ($related as $table => $id) {
                /** @var Model $model */
                $model = $this->framework->getAdapter($adapter->getClassFromTable($table));

                // Check if the related records still exist
                if (null !== $model->findMultipleByIds($id)) {
                    $delete = false;
                    break;
                }
            }

            if ($delete) {
                $token->delete();
            } else {
                // Prolong the token for another 3 years if the related records still exist
                $token->removeOn = strtotime('+3 years', (int) $token->removeOn);
                $token->save();
            }
        }
    }
{@inheritdoc}