<?php
/**
 * COmanage Database Setup Already Command, shared between Match and Registry
 *
 * Portions licensed to the University Corporation for Advanced Internet
 * Development, Inc. ("UCAID") under one or more contributor license agreements.
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.
 *
 * UCAID licenses this file to you under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @link          http://www.internet2.edu/comanage COmanage Project
 * @package       match
 * @since         COmanage Common v1.0.0
 * @license       Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
 */

/**
 * THIS FILE SHOULD BE MASTERED IN THE COMMON REPOSITORY.
 */

declare(strict_types = 1);

namespace App\Command;

use Cake\Console\Arguments;
use Cake\Console\Command;
use Cake\Console\ConsoleIo;
use Cake\ORM\TableRegistry;

class DatabaseSetupAlreadyCommand extends Command {
  /**
   * Test Connectivity to the Database
   *
   * @since  COmanage Match v1.0.0, COmanage Registry v5.0.0
   * @param  Arguments $args Command Arguments
   * @param  ConsoleIo $io   Console IO
   * @throws RuntimeException
   */

  public function execute(Arguments $args, ConsoleIo $io) {
    $metaTable = TableRegistry::get('Meta');

    try {
      $metaTable->findById('1')->firstOrFail();
      $io->out("Match database is setup");
      return 0;
    } 
    catch(\Exception $e) {
      $io->out("Match database is not setup");
      return 1;
    }
  }
}